what is exception propagation?
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
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.
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
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/