语法是try…catch…finally
try {
//可能发生异常的代码块
System.out.println(c[3]);
System.out.println(a/b);
}catch(ArrayIndexOutOfBoundsException e2) {//可以捕获的异常1
//处理异常1的代码
System.out.println("数组越界");
}catch(ArithmeticException e1) {
System.out.println("0不能做除数");
}finally{
//处理完所有异常后一定会执行的代码。
//在这里注意的是。如果在这里没有出现异常,最终也会执行这行代码
}
try块
表示程序正常要执行的代码部分,但是可能会出现一些非预期的异常影响程序的运行。所以try的作用类似于对一块代码有目的性的检查一样。这样,异常一旦出现,JVM将会生成一个异常对象,这个异常对象将会被后面相应的catch块捕获。
catch块
异常捕获块,当程序执行try块引发异常时,这个异常对象将会被此处相应的catch块捕获,并作出相应处理。
finally块
块代表异常处理流程中总会执行的代码块,不论前面异常与否(catch块执行与否),finally块总是会执行的。
try…catch…finally用法
public static void main(String[] args) {
int a = 1;
int b = 0;
int[] c = {1,2,3};
// System.out.println(a/b);//Exception in thread "main" java.lang.ArithmeticException: / by zero at genericity.ExceptionTest.main(ExceptionTest.java:8)
// System.out.println(c[3]);//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at genericity.ExceptionTest.main(ExceptionTest.java:9)
try {
System.out.println(c[3]);
System.out.println(a/b);
}catch(ArrayIndexOutOfBoundsException e2) {
System.out.println("数组越界");
}catch(ArithmeticException e1) {
System.out.println("0不能做除数");
}finally {
System.out.println("结束");
}
}
注意:
- 存在多个catch块时,要按照括号中包裹错误的范围,由小到大排列。便于做到更精确的处理,这也正是不直接用Exception的原因。
- 多个catch不会同时执行,当try块执行到第一行异常代码时便会直接跳到catch块。因此每次处理的结果,都是try代码块中第一个异常匹配到的对应catch块中的处理方案,以及finally块中的代码。
throws关键字
throws用于在方法签名中声明抛出一个或多个异常类,throws关键字后可以紧跟一个或多个异常类
public class ExceptionTest {
public static void main(String[] args) {
try {
run();
}catch(ArithmeticException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void run() throws ArithmeticException {
System.out.println(1/0);
}
}
throw关键字
throw用于手动地抛出异常对象。throw后面需要一个异常对象。此例即为我们常听到的自定义异常。
public class ExceptionTest {
public static void main(String[] args) {
try {
run();
}catch(ArithmeticException e) {
System.out.println(e.getMessage());
}
}
public static void run() throws ArithmeticException {
throw new ArithmeticException("自定义异常:0不能为除数");
}
}
//-------------------------------或-------------------------------
public class ExceptionTest {
public static void main(String[] args) {
run();
}
public static void run() throws ArithmeticException {
try {
throw new ArithmeticException("自定义异常:0不能为除数");
} catch(Exception e) {
System.out.print(e);
}
}
}
注意:
- throw的作用类似于return,表示着当前方法的终结。其后面不能再去写代码。
- 自行抛出异常,必须处于try块中,或带throws声明的方法中。
- 上面例子中main()的异常范围必须大于run()方法中throws的范围;而run()方法中throws的范围又必须大于throw的范围。
- 自定义异常时只能继承Exception或者 RuntimeException
Runtime
自行抛出Runtime异常,既可以显示捕获该异常,也可以不用理会该异常,交给方法调用者处理
public static void main(String[] args) {
run();
}
public static void run() {
throw new RuntimeException("0不能为除数");
}
结语
总的来说,不论是throw、throws、runtime,都是把异常上抛,抛给调用者去解决,如此逐级上抛,最终是被异常类对象中的方法解决掉(先抛去自定义异常和catch块不说)。因此,为了避免在其他地方调用方法时,代码被大量的try…catch…语句侵入。在写方法时,能预见的异常,能用逻辑语句解决的问题,就千万不要用异常。
另外,从上面的实例可以看出,明显的runtime要比try…catch…好用。但为什么还是保留try…catch…用法?
因为有些操作必不可少的需要try…catch…finally。比如流操作,不论所操作的文件存在与否(此处就以找不到处理文件这个异常举例),在执行到最后,必须在finally中关闭流。
此时可能又产生了问题。为什么不把关闭流操作放在对应异常处理的catch块中呢?
因为我们在写代码时不一定只写一个catch块,如果程序运行时出现了其他异常,而正好这个异常的catch块又在写有关闭流操作的catch块前面。那还是没有关闭流,除非在每一个catch块里都写一句关闭流的代码。
为什么不把关闭流操作放在整个异常捕获的外面呢?
这样的话流对象就得在try块后面写。如此一来就要提升流对象的作用域。