the principle and introduction of Atitit anomaly mechanism and exception handling
1.1. atitit. The design principle of the anomaly mechanism . docx1
1.2. termination mode vs Recovery mode (ASP1
1.3. Handling exceptions for reflection methods 1
1.4. Re- throwing exceptions and exception chains 2
1.5. Finally handling of abnormal loss 3
1.1.
Atitit. design principle of anomaly mechanism . docx
The JVM looks at exception essence . Java 's ex design throry
1.2.
termination mode vs Recovery Mode(ASP
Java's handling of exceptions takes Termination Mode , once a problem occurs, the program will not be able to continue to execute, corresponding to the Recovery Mode is that when an exception is thrown, the program can continue, rather than terminate. In Java if we want to use the recovery model, we need to put the try block in the while loop until it is satisfied, but this is obviously not reliable, and we do not advocate. So when the current method terminates, we can only keep the program in a different direction in the exception handling block, and in what direction, depending on the specific implementation
1.3.
Handling Exceptions for reflection methods
public     void   throwexv3 (Throwable Span style= "Font-family:consolas; Color: #6a3e3e; Font-size:12.0000pt ">e ,string msg ) {
if (e instanceof invocationtargetexception)
{
e = e . Getcause ();
}
if ( e instanceof runtimeexception)
{
Throwable E3 = e . Getcause ();
runtimeexception runtimeexception = new runtimeexception ( msg e3 " Span style= "Font-family:consolas; Font-size:12pt "");
throw runtimeexception;
Throw (RuntimeException) e;
}
Else
throw new runtimeexception (msg,e);
}
1.4.
re-throwing exceptions and exception chains
Sometimes, after catching an exception, we might not be able to handle the exception where it was captured, and we need to throw it back:
catch (Exception e) {
Throw e;
}
This has the advantage that we can give the exception to the upper level of the environment processing, but there is a problem, the thrown exception carries the information, that is, the Printstacktrace () method shows the original exception throws the point of the call stack information, rather than the re-throw point of information, The invocation information of the re-throw point is then masked. If you want to update the re-throw point information into this exception call stack, you can use the Fillinstacktrace () method:
catch (Exception e) {
Throw E.fillinstacktrace ();
}
Then the current call stack information is updated to the exception object, there is a situation, there will be a similar loss phenomenon:
catch (Exception e) {
throw new Exception ();
}
1.5.Finally Abnormal lossthe processing
we take the outer try to look at the first level of the process of processing, in this try to happen two exceptions, but we can only get the exception information thrown from finally, and in the F () method of the exception information is lost, this situation we call the previous The exception was suppressed . This before JDK1.7 also need our own code to solve this problem, after JDK1.7, new added two methods to help us to solve this problem, that is addsuppressed (Throwable exception) and getsuppressed (), for the resolution of the above problems:
public static void Main (string[] args) {try {test test = new test (); Exception Exception = null; try {test.f ();} catch (Veryimportantexception e) {exception = e;} finally {try {test.dispose ();} catch (Otherexcept Ion e) {if (exception! = null) {exception.addsuppressed (e);} else {exception = e;}} if (exception! = null) {throw E Xception; }}} catch (Exception e) {System.out.println (e); for (Throwable throwable:e.getsuppressed ()) {System.out.println (thro wable); } } }
Java Exception details -weisg81 's Column - Blog channel -CSDN.NET.html
author:: Nickname :Old Wow's claws( Full Name::AttilaxAkbar Al Rapanui Attilaksachanui)
Kanji Name: Etila ( Ayron) , email:[email protected]
reprint Please indicate source: http://www.cnblogs.com/attilax/
Atiend
The principle and introduction of Atitit anomaly mechanism and exception handling