问题
Hello i am not that familiar with Exception Handling in Java so: As the Topic says in a basic try/catch block, when i catch an Exception in the Try block, when is the Program flow interrupted?
try{
//some code that raises an Exception
}catch(Exception e){
// react to interrupt or continue program flow
}finally{
// always done after the catch
}
//when is this code executed?
The finally statement is always executed after a try catch, so what has to be done in the catch part to either interrupt the program or let the program continue?
回答1:
Instruction flow is interrupted when any exception occurs anywhere.
If the exception occurs in a try
range, and the exception class is "assignable to" the class specified in the catch
clause, then control is transferred to the beginning of the catch block. (If the exception class is not "assignable" then the exception simply "bubbles up" to the next outer try
block or the thread EH.)
If the catch
block does not throw an exception (or rethrow the original one) then the exception is considered "handled", and execution continues immediately after the list of catch
clauses for that try
. This may mean that flow goes into the finally
block before continuing at the end of the finally
.
If the exception is not caught, or an exception is (re)signalled in the catch
block, then any following finally
block is executed, before the exception "bubbles up".
If a return
is executed in the try
or catch
block then any finally
is executed before the return
takes effect.
回答2:
If you catch the exception in the catch block, and the finally block doesn't throw any exception, the statements after the finally block will be executed immediately after the finally block is executed.
If you don't catch the exception in the catch block, or you throw a new exception in either the catch block or the finally block, the statements after the finally block will not be executed, and the next code that will be executed is the most immediate enclosing catch block that can handle the thrown exception.
回答3:
Catch()
block is to notify/handle the exceptions.
Quoting from docs,
The catch block contains code that is executed if and when the exception handler is invoked. The runtime system invokes the exception handler when the handler is the first one in the call stack whose ExceptionType matches the type of the exception thrown. The system considers it a match if the thrown object can legally be assigned to the exception handler's argument.
and
As the Topic says in a basic try/catch block, when i catch an Exception in the Try block, when is the Program flow interrupted?
Catch blocks are meant to handle the exceptions. So if there are no errors Catch
block will not be executed, however finally()
will be executed
回答4:
try {
System.out.println("Throw exception");
int i = 5/0;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
System.out.println("Doubt solved if line prints");
}
System.out.println("After exception");
here the code try it by your self i think there will not be need of explanation after execution of this till tile exception occur it will directly go to the catch and even then it will go to the execution of finally and then further code.
回答5:
Execute following code to understand flow of exception for exception.
try {
//line 1
//line 2
throw new Exception(); //line 3
// line 4
// line 5
} catch (Exception e) {
// if exception occur at line 3, line 4 & 5 will not executed because of interrupt in execution flow.
// since your program is interrupted when exception occur.
System.out.println("Catch block will execute after exception !");
}finally{
System.out.println("Finally block will execute after exception !");
}
// your program will continue its execution.
System.out.println("Code after the finally block also exectute after the exception !");
Output
Catch block will execute after exception !
Finally block will execute after exception !
Code after the finally block also exectute after the exception !
来源:https://stackoverflow.com/questions/26994308/java-try-catch-when-is-the-program-flow-interrupted