Exception thrown in catch and finally clause
2013-12-19 21:27
465 查看
Based on reading your answer and seeing how you likely came up with it, I believe you think an "exception-in-progress" has "precedence". Keep in mind:
When an new exception is thrown in a catch block or finally block, the current exception is aborted (and forgotten) and the new exception is thrown. The new exception starts unwinding up the stack just like any other exception, aborting out of the current block (the catch or finally block) and subject to any applicable catch or finally blocks along the way.
Note that applicable catch or finally blocks includes:
When a new exception is thrown in a catch block, the new exception is still subject to that catch's finally block, if any.
Now retrace the execution remembering that, whenever you hit
当在catch和finally块中产生新异常, 当前的异常失效并被丢弃,新异常会被后续的catch和finally块处理。
注意: 一个新异常在catch块产生时,会被这个catch匹配的finally处理。
above code will print:
333
When an new exception is thrown in a catch block or finally block, the current exception is aborted (and forgotten) and the new exception is thrown. The new exception starts unwinding up the stack just like any other exception, aborting out of the current block (the catch or finally block) and subject to any applicable catch or finally blocks along the way.
Note that applicable catch or finally blocks includes:
When a new exception is thrown in a catch block, the new exception is still subject to that catch's finally block, if any.
Now retrace the execution remembering that, whenever you hit
throw, you should abort tracing the current exception and start tracing the new exception.
当在catch和finally块中产生新异常, 当前的异常失效并被丢弃,新异常会被后续的catch和finally块处理。
注意: 一个新异常在catch块产生时,会被这个catch匹配的finally处理。
public class A { public static void main(String[] args) { try{ exceptionFunction(); }catch(Exception e){ // print catches which Exception 111, 222, or 333. System.out.println(e.getMessage()); } } public static void exceptionFunction(){ try{ throw new IllegalArgumentException("111"); }catch(Exception e){ throw new IllegalArgumentException("222"); }finally{ throw new IllegalArgumentException("333"); } } }
above code will print:
333
相关文章推荐
- Exception thrown in catch and finally clause
- [转载] Basic try-catch-finally Exception Handling in Java
- Error Handler/Exception Clause/Try ... Catch ... Finally
- finally_clause_and_return_clause_in_java
- Await in Catch and Finally
- MySQLIntegrityConstraintViolationException: Column 'ID' in where clause is ambiguous
- mongo启动异常-mongodb exception in initAndListen
- Docker安装MySQL遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
- mysql错误:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ga
- Difference between throw and throws in Exception handling - Java Example
- [mongoDB]exception in initAndListen: 20 Attempted to create a lock file on a read-only directory:
- mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
- 使用maven打包jar后 执行jar包问题,Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefi
- linux上,mysql使用聚合函数group by 时报错:SELECT list is not in GROUP BY clause and contains nonaggre的问题
- How to handle exception in managed code and unmanaged code
- mysql:Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column
- Docker安装MySQL遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
- mongodb 错误代码 exception in initAndListen: 12596 old lock file, terminating
- MySQLIntegrityConstraintViolationException: Column 'ID' in where clause is ambiguous
- Unreachable catch block for UserExistException. This exception is never thrown from the try statemen