Java 简明教程
Java - Exception Propagation
Exception Propagation
Exception 传播是指异常事件从 nested try 或嵌套方法调用移动。一个 try 块可以嵌套在另一个 try block 内。类似地,一个 method 可以调用另一个方法,其中每个方法都可以独立处理异常或可以抛出已检查/未检查异常。每当在嵌套的 try 块/方法内引发异常时,其异常就会推入堆栈。该异常会从子 try 块或子方法传播到父 try 块或父方法,依此类推。
Exception propagation refers to movement of exception event from nested try or nested methods calls. A try block can be nested within another try block. Similarly a method can call another method where each method can handle exception independently or can throw checked/unchecked exceptions. Whenever an exception is raised within a nested try block/method, its exception is pushed to Stack. The exception propagates from child to parent try block or from child method to parent method and so on.
Syntax - Nested Try Block
嵌套 catch 块的语法如下所示 −
The syntax for nested catch blocks looks like the following −
try { // parent try block
try { // child try block
}
catch(ExceptionType1 e1){ // child catch block
}
} catch (ExceptionType2 e1) { // parent catch block
}
Syntax - Nested Method Calls
嵌套方法调用的语法如下所示 −
The syntax for nested method calls looks like the following −
method1(){ // parent method
try { // parent try block
method2();
} catch (ExceptionType2 e1) { // parent catch block
}
method2(){ // child method
// code to throw Exception
// this exception will be handled by parent method
}
之前的语句演示了两个 try/catch 块和方法,但你还可以拥有任意数量的块和方法。如果异常发生在受保护的子代码中,则会将异常抛出到子列表的 catch 块。如果抛出的异常的数据类型与 ExceptionType1 相匹配,则会在那里被捕获。如果没有,异常会传递到父 catch 语句。这种情况会一直持续,直到捕获异常或遍历所有 catch 为止,在这种情况下,当前方法会停止执行,并将异常向下抛出到调用堆栈中的前一个方法。
The previous statements demonstrate two try/catch blocks and methods, but you can have any number of them. If an exception occurs in the protected child code, the exception is thrown to the catch block of the child list. If the data type of the exception thrown matches ExceptionType1, it gets caught there. If not, the exception passes up to the parent catch statement. This continues until the exception either is caught or falls through all catches, in which case the current method stops execution and the exception is thrown down to the previous method on the call stack.
Rules for Exception Propagation in Java
-
Child catch block should have specific exception for better code clarity. Parent catch block can have more generic exception handled so that if child catch block is not able to handle the exception then parent catch block can handle it.
-
There in no restriction on exception hiearchy to be used in child vs parent catch block.
-
If a exception is handled correctly in child catch block, then in parent, another exception can be raised and handled.
Java Exception Propagation Example
以下是显示从子到父的异常事件传播的代码段。在此示例中,我们在子方法中通过将值除以 0 来创建错误。子方法抛出异常。现在在父方法中,在 try 块中,我们处理异常并打印错误消息。
Here is code segment showing exception event propagation from child to parent. In this example, we’re creating an error by dividing a value by 0 in a child method. The child method throws the exception. Now in parent method, in try block, we’re handling the exception and printing the error message.
package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
int a = 3;
int b = 0;
try {
System.out.println("result:" + divide(a,b));
}catch(ArithmeticException e) {
System.out.println(e.getMessage());
}
}
private static int divide(int a, int b) {
return a / b;
}
}
/ by zero
More Examples
Example 1
以下是显示从子到父的异常事件传播的代码段。在此示例中,我们在子方法中通过将值除以 0 来创建错误。子方法抛出异常。现在在父方法中,我们不处理异常。JVM 会拦截异常并打印错误消息。
Here is code segment showing exception event propagation from child to parent. In this example, we’re creating an error by dividing a value by 0 in a child method. The child method throws the exception. Now in parent method, we’re not handling the exception. The JVM will intercept the exception and prints the error message.
package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
int a = 3;
int b = 0;
System.out.println("result:" + divide(a,b));
}
private static int divide(int a, int b) {
return a / b;
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.tutorialspoint.ExcepTest.divide(ExcepTest.java:12)
at com.tutorialspoint.ExcepTest.main(ExcepTest.java:8)
Example 2
以下是显示异常事件传播在子自身停止而不是流向父进程的代码段。在此示例中,我们在子方法中通过将值除以 0 来创建错误。子方法正在处理异常。现在在父方法中,我们没有得到任何异常。
Here is code segment showing exception event propagation to stop within child itself instead of flowing to parent. In this example, we’re creating an error by dividing a value by 0 in a child method. The child method is handling the exception. Now in parent method, we’re not getting any exception.
package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
int a = 3;
int b = 0;
System.out.println("result:" + divide(a,b));
}
private static int divide(int a, int b) {
try {
return a / b;
}catch(ArithmeticException e) {
System.out.println(e.getMessage());
}
return 0;
}
}
/ by zero
result:0