1. An exception chain is provided in the Java constructor. That is, we can continuously connect exceptions into an exception chain through the constructor...
The reason why exception connection is required is because of the understanding of the code, as well as the maintainability of the reading and program...
We know that every time we throw an exception, we need to try catch... Then isn't the code very bloated...
If we can concatenate exceptions into an exception connection, then we only catch our wrapping exceptions, we know that RuntimeException and its derived classes can be automatically caught and processed by jvm without trying catch..
Of course, we can define our own exception class to be derived from RuntimeException, and then pass one-level wrapper. If the exception appears, JWM directly outputs cause through our custom RuntimeException
(Reason) is our exception chain... Therefore, all our exceptions are output, which reduces a lot of exception handling code. . .
Only Throwable ---> Exception RuntimeException Error provides a mechanism for constructing methods to implement exception chains. . . Other exceptions need to be used through initCause
Construct an exception connection...
The following piece of code is a simple example of exception connection... You can print exceptions that occur throughout the entire program. .
public class TestT { public static void a() throws Exception{ //Top an exception to handle the superior try { b() ; } catch (Exception e) { throw new Exception(e) ; } } public static void b() throws Exception{ //Top an exception to handle the superior try { c() ; } catch (Exception e) { throw new Exception(e); } } public static void c() throws Exception { //Top an exception to handle the superior try { throw new NullPointerException("c Null pointer exception in exception chain..") ; } catch (NullPointerException e) { throw new Exception(e) ; } } public static void main(String[]args){ try { a() ; } catch (Exception e) { e.printStackTrace(); } }}2. try catch ... finally there is a vulnerability, which is the exception. For example, three try catches are nested together. The two try catches inside can omit catch....Turn directly try finally..
Looking at the following code, we found that 2 exception information was missing
public class MyTest { public void open() throws Exception{ throw new Exception(){ public String toString() { return this.getClass().getName()+"CeryImmportException"; }; } ; } public void close() throws Exception{ throw new Exception(){ public String toString() { return this.getClass().getName()+"close Exception" ; }; } ; } public void three() throws Exception{ throw new Exception(){ public String toString() { return this.getClass().getName() + "three" ; }; } ; } ; } public static void main(String[]agrs){ MyTest mt=new MyTest() ; try{ try{ try{ mt.open(); } finally { System.out.println("delete open"); mt.close() ; } } finally{ System.out.println("delete close"); mt.three() ; } } catch(Exception ex){ ex.printStackTrace(); } } }The above article briefly discusses java exception chain and exception loss is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.