What is exception propagation? I tried to Google it, but couldn't find satisfactory results. Preferably explain this in terms of Java.
问题:
回答1:
It's explained, surprisingly, in the Java tutorial page about exceptions.
An exception propagates from method to method, up the call stack, until it's caught. So if a()
calls b()
, which calls c()
, which calls d()
, and if d()
throws an exception, the exception will propagate from d to c to b to a, unless one of these methods catches the exception.
回答2:
Short answer : Uncaught exceptions are propagated in the call stack until stack becomes empty, this propagation is called Exception Propagation.
Long answer : After a method throws an exception, the runtime system searches the call stack for a method that contains a block of code(exception handler) that can handle the exception. The search begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the handler. Also, there's a note-worthy point:
Lets say, we have a chain of methods where method3() calls method2() and method2() calls method1(). So when
1) An exception occurs in the method3() and in method3() we don’t have any exception handler.
2) Uncaught exception will be propagated downward in stack i.e it will check appropriate exception handler in the method2().
3) Again in method2 if we don’t have any exception handler then again exception is propagated downward to method1() where it finds exception handler.
Example:
class ExceptionPropagation{
void method3(){
int result = 100 / 0; //Exception Generated
}
void method2(){
method3();
}
void method1(){
try{
method2();
} catch(Exception e){
System.out.println("Exception is handled here");
}
}
public static void main(String args[]){
ExceptionPropagation obj=new ExceptionPropagation();
obj.method1();
System.out.println("Continue with Normal Flow...");
}
}
Output :
Exception is handled here
Continue with Normal Flow...
Only unchecked exceptions are propagated. Checked exceptions throw compilation error
[1] http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
[2] http://www.c4learn.com/java/java-exception-propagation/
回答3:
Whenever methods are called stack is formed and an exception is first thrown from the top of the stack and if it is not caught, it starts coming down the stack to previous methods until it is not caught. If exception remains uncaught even after reaching bottom of the stack it is propagated to JVM and program is terminated.
unchecked exceptions are automatically propagated in java. Program >
public class ExceptionTest {
public static void main(String[] args) {
method1();
System.out.println("after calling m()");
}
static void method1() {
method2();
}
static void method2() {
method3();
}
static void method3() {
throw new NullPointerException();
}
}
For propagating checked exceptions method must throw exception by using throws keyword. Program >
public class ExceptionTest {
public static void main(String[] args)
throws FileNotFoundException {
method1();
System.out.println("after calling m()");
}
static void method1() throws FileNotFoundException{
method2();
}
static void method2() throws FileNotFoundException{
method3();
}
static void method3() throws FileNotFoundException{
throw new FileNotFoundException();
}
}
Propagating unchecked exception (NullPointerException) >
Propagating checked exception (FileNotFoundException) using throws keyword >
From :http://www.javamadesoeasy.com/2015/05/exception-propagation-in-java-deep.html
回答4:
when an exception happens, propagation is a process in which the exception is being dropped from to the top to the bottom of the stack and in to the calling chain to be cut and If not caught there, the exception again drops down to the previous method, and so on until it gets caught or until it reach the very bottom of the call stack.This is called exception propagation.
For Example assume our stack is:
c()
b()
a()
main()
If an exception happens in c() method and if it is not handled it will be propagated to previous b() method and if it is not handled there, again it is propagated to a() method where exception is handled and so on.
Exception can be handled in any method in call stack either in main() method, a() method, b() method or c() method.
Reply to Thread
回答5:
Let's say you have an object that calls another object and then that one calls another one. If a exception is thrown in any of the called objects and it is not caught, then the exception propagates to the calling method (and crashes the application if is not caught anywhere).
class MyClass{
void myMethod(){
A a = new A();
a.doSomething(0);
}
}
class A{
double doSomething(int n){
return 1/n;
}
}
If myMethod
method is executed an exception will be thrown in doSomething
method of object A
, and the exception propagates to the invoking method in the stack (so the exception propagates in this example to myMethod
of myClass
).
回答6:
In constructing a program, the place at which an error occurs is not the best place to handle it i.e. the error is handled at the place rather than the place where it has been occured.
回答7:
If why we need this feature or when do we use it is the case, which in turn can answer the purpose of this feature,
Of several things, exception back propagation back along method call hierarchy enables the concept @transaction which is a automatically enabled using aspect oriented programming concept. (@transaction helps to DB rollback in spring framework). Here exception propagation till method declared @transaction in your call hierarchy is must to automatically trigger DB rollback and later you can handle the exception. Not related to question but @transaction works only for runtime exceptions.