Java
的异常处理有两种方式:
- 默默解决。使用try...catch
。
- 甩锅。使用throw
和throws
。
通常,我们喜欢用try/catch
捕获异常,然后对异常进行处理。但是有时候,我们也会不予理会,直接将异常扔出去,让调用者自己去处理异常。当然对调用者来说,它也有两种同样的选择:自己处理掉,或者再往外扔,交给自己的调用者,由新的调用者继续背锅。
以下是几种正常与不正常的异常处理方式示例,从中可以看出异常处理的流程:
测试类一
正常抛出异常。声明throws
且throw
:
package Exception;
public class ExceptionThrow {
public static void foo() throws Exception {
throw new Exception("throw my exception.");
}
}
测试类二
声明throws
,但是不throw
(不抛出异常):
package Exception;
public class ExceptionNotThrow {
public static void bar() throws Exception {
;
}
}
测试类三
异常已经由本方法已处理,但是仍声明throws
:
package Exception;
public class ExceptionHandledButThrow {
public static void foobar() throws Exception {
try {
ExceptionThrow.foo();
} catch (Exception e) {
System.out.println("Catch an exception: " + e.getMessage());
}
}
}
测试类四、五:
package Exception;
import java.util.concurrent.TimeoutException;
public class InnerException {
public static void foo() throws TimeoutException {
throw new TimeoutException("inner timeout exception");
}
}
package Exception;
public class OuterException {
public static void bar() throws Exception {
try {
InnerException.foo();
} catch (NullPointerException e) {
e.getMessage();
System.out.println("Only catch NullPointerException.");
}
}
}
测试类:
package Exception;
public class Main {
public static void main(String[] args) {
System.out.println("=========Case 1=========");
try {
ExceptionThrow.foo();
System.out.println("=>1: Should never be printed!!!");
} catch (Exception e) {
if (e.getMessage().contains("my")) {
System.out.println("Exception happens: " + e.getMessage());
// e.printStackTrace();
} else {
System.out.println("=>2: Should never be printed!!!");
}
} finally {
System.out.println("=>3: Should always be printed!!!");
}
System.out.println("=========Case 2=========");
try {
ExceptionNotThrow.bar();
System.out.println("=>1: Should be printed!!!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("=>2: Should never be printed!!!");
} finally {
System.out.println("=>3: Should always be printed!!!");
}
System.out.println("=========Case 3=========");
try {
ExceptionHandledButThrow.foobar();
System.out.println("=>1: Should be printed!!!");
} catch (Exception e) {
System.out.println("=>2: Should never be printed!!!");
}
System.out.println("=========Case 4=========");
try {
OuterException.bar();
} catch (Exception e) {
System.out.println("In main: " + e.getMessage());
}
}
结果
=========Case 1=========
Exception happens: throw my exception.
=>3: Should always be printed!!!
=========Case 2=========
=>1: Should be printed!!!
=>3: Should always be printed!!!
=========Case 3=========
Catch an exception: throw my exception.
=>1: Should be printed!!!
=========Case 4=========
In main: inner timeout exception
分析
- Case1:
try
在捕获到异常之后,直接进入catch
,运行catch
之内的部分。catch
可以根据异常的情况,进行不同的处理逻辑。 - Case2:如果
try
捕获不到异常,将继续执行try
后面的部分,catch
部分则直接跳过。(eg:ExceptionNotThrow.bar()
,只声明throws
却不throw
,相当于方法永不会抛出异常,则处理这个方法异常的try
永远都不会捕获到异常) - Case3:如果本方法(eg:
ExceptionHandledButThrow.foobar()
)已经处理了异常,还声明throws
,则调用者同样在try
块里永远都不会捕获到异常(因为异常已经被处理了),但是调用者必须有try/catch
,因为所调用的方法(foobar()
)虽然不抛异常,但是坑爹地声明了throws
。 - Case4:如果本方法没有
catch
住被调用者抛出的异常,则必须将异常继续throws
出去,否则会报错。