Exception 是在程序执行过程中发生的一些不希望发生的事情,这些事情如果不被好好处理,就会导致奇怪的结果或者是程序终结。Exception Handler是那些当异常发生时处理这些异常的代码。所以这里只讲三个事儿:
Exception 是在程序执行过程中发生的一些不希望发生的事情,这些事情如果不被好好处理,就会导致奇怪的结果或者是程序终结。Exception Hander是那些当异常发生时处理这些异常的代码。java和javascript都用try/catch来处理异常。
exception在java里也是个object。
来个图先
图片来源: http://voyager.deanza.edu/~hso/cis35a/lecture/java13/intro/hier.html
它爸爸是Throwable
(面试会考,敲黑板)。它还有个兄弟叫Error
。
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
虽然都发生在runtime,但是error不被建议去handle。 大部分情况下即使是加了catch也无法修复。而RuntimeException可以去handle。
有一些方法在声明的时候就声明扔出一个exception。如果这个exception是checked exception,则调用这个方法的时候就必须handle它。
checked exception就像一个炸弹,如果说方法A的某处扔出一个炸弹,或者从别处接到一个炸弹(调用了另一个扔出exception的方法), 有两种解决方案:
throws
.)如果采用第一种方案,当方法B调用方法A的时候,方法A已经把炸弹拆了,方法B不用担心任何事情。
如果采用第二种方法,方法B调用方法A的时候知道同时要接到一个炸弹,于是它有两种解决方案,拆了,或者继续throws。
一个常见的数组越界exception。是个unchecked的exception,因为编译时没有要求handle,所以是个常见的新手 runtime异常。(不要求被handle,但是如果想handle也是可以的。)
import java.io.*;
public class ExceptionTest1 {
public static void main(String args[]) {
try {
int a[] = new int[2];
System.out.println(a[3]); //数组越界访问
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);//虽然没有做有意义的事情,但是阻止了程序死在半截。
}
System.out.println("run successfully");
}
}
一个try可以跟着好几个catch, 为了分开处理不同的错误:
try {
//doing something
} catch (Exception1 e) {
// handle the first type exception
System.out.println("Exception1 happened")
} catch (Exception2 f){
// handle the first type exception
System.out.println("Exception2 happened")
}
一个exception发生之后,如果是Exception1类型的,就会被第一个handle,如果不是Exception1类型的就会接着往下找catch,如果是Exception2类型的,就会被第二个catch块handle。
如果Exception1和Exception2是父子关系,则儿子要先被检测,因为如果爸爸先被检测,就永远也到不了儿子那个catch块了。
finally block 是无论如何也会发生的一个block。 catch里的代码如果不发生异常就不会被执行,但是finally里面的代码无论如何都会执行。(除非是在try或者catch里面用System.exit(1)
结束jvm。)通常用来关闭文件。
try {
//doing something
} catch (Exception e) {
//handle the exception
}finally {
//一定会被执行的代码。
}
}
}
另外, try-finally
也是合法的。
用throws
关键词
//m1抛出了一个异常
void m1 throws FileNotFoundException(){
//大概是想读一些文件,又不想考虑如果文件找不到咋整。(甩锅)
}
//m2调用了m1,相当于从m1手里接到了这个异常,自己不处理,继续往外甩。
void m2 throws FileNotFoundException(){
m1();//想用m1方法,m1声明如果想用我就要考虑文件找不到的情况。
//但是m2依然不想考虑文件找不到咋整。
}
下面代码由于接到了exception没有正确handle而产生编译错误:
import java.io.File;
import java.io.FileReader;
//会有编译错误
public class ReadFileTest {
public static void main(String args[]) {
getFile();
}
public static void getFile(){
File file = new File("file.txt");
FileReader fr = new FileReader(file);//error: unreported exception FileNotFoundException; must be caught or declared to be thrown
}
}
因为FileReader的constructor throws了一个异常
public FileReader(String fileName) throws FileNotFoundException
所以当getFile方法调用FileReader的时候必须handle这个异常。
下面是一个例子用上面两种方法处理异常:
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class ReadFileTest {
public static void main(String args[]) {
getFile1();
System.out.println("=============");
try{
// 因为getFile2又把exception甩出来了,所以main方法得处理它。
getFile2();
}catch(FileNotFoundException e){
System.out.println("Exception handled in main");
}
}
//getFile1选择了自己处理,不麻烦调用它的方法
public static void getFile1() {
try{
File file = new File("file.txt");
FileReader fr = new FileReader(file);
}catch(FileNotFoundException e){
System.out.println("Exception handled in getfile1");
}
}
//getFile2选择了不处理,继续throws
public static void getFile2() throws FileNotFoundException{
File file = new File("file.txt");
FileReader fr = new FileReader(file);
}
}
运行结果:
Exception handled in getfile1
=============
Exception handled in main
一般来讲,所有的exception到了main方法这里都应该已经被解决了。如果,main
方法也不负责任的往外扔。。。
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class ReadFileTest {
public static void main(String args[]) throws FileNotFoundException{
getFile1();
System.out.println("=============");
getFile2();
}
public static void getFile1() {
try{
File file = new File("file.txt");
FileReader fr = new FileReader(file);
}catch(FileNotFoundException e){
System.out.println("Exception handled in getfile1");
}
}
public static void getFile2() throws FileNotFoundException{
File file = new File("file.txt");
FileReader fr = new FileReader(file);
}
}
这样做是可以的。。。在没有文件的情况下(触发exception)运行结果:
Exception handled in getfile1
=============
Exception in thread "main" java.io.FileNotFoundException: file.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileReader.(FileReader.java:72)
at ReadFileTest.getFile2(ReadFileTest.java:21)
at ReadFileTest.main(ReadFileTest.java:8)
extends exception和它的孩子们呀
1. class New1Exception extends Exception { } // 创建一个checked exception
2. class NewException extends IOExcpetion { } // 创建一个跟IO相关的exception
3. class NewException extends RuntimeException { } // 创建一 UnChecked exception
来个例子(就是这么简单):
import java.io.*;
class MyException extends Exception
{
public MyException(String s)
{
super("a MyException happens: " + s);
//还可以做一些其他的事
}
}
throw是在方法或者代码块里面,用来扔炸弹 抛出异常。
throws是在方法声明的时候,说明这个方法抛出了异常。
用刚才的MyException写个例子:
import java.io.*;
public class Main
{
public static void main(String args[])
{
try {
throwTest();
} catch (MyException e) {
System.out.println("Caught something");
System.out.println(e.getMessage());
}
//throwTest2是一个正常的方法,扔的exception被自己handle了。
throwTest2();
//另用一个block测试test3,
//如果写在同一个block,throwTest()抛出异常后,throwTest3()不会被执行
try {
throwTest3();
} catch (MyException e) {
System.out.println("Caught something");
System.out.println(e.getMessage());
}
}
// 自己不handle,扔出来之后继续在方法声明里告诉调用自己的方法需要handle
public static void throwTest() throws MyException {
throw new MyException("something");
}
//自己handle了,变成一个正常的方法
public static void throwTest2() {
try {
throw new MyException("somethingElse");
} catch (MyException e) {
System.out.println("Caught somethingElse");
System.out.println(e.getMessage());
}
}
//自己handle了,在catch里继续往外扔。
public static void throwTest3() throws MyException{
try {
throw new MyException("somethingElse2");
} catch (MyException e) {
//一个非常不负责任的catch,但是是合法的。
throw e;
}
}
}
输出:
Caught something
a MyException happens: something
Caught somethingElse
a MyException happens: somethingElse
Caught something
a MyException happens: somethingElse2