Analysis of Java Abnormal Mechanism
1. First define a class before analyzing:
1.1 Defining anomalous class
Public class a extends exception {
Public a () {
}
}
Define class public class c1 {
Public C1 () {
}
Public Final Void Print () throws a {
Try {
Throw new a ();
}
Finally {
System.out.println ("c1.finally");
// Return;
}
}
}
Define the test class:
Public class c {
Public c () {
}
INT test () throws a {// test function, then rewrite
Throw new a ();
}
Public static void main (String [] args) {
C c = new c ();
Try {
System.out.println (C. Test ());
System.out.println ("main.try");
}
Catch (a e) {
System.out.println ("main.catch");
}
}
}
Java exception summary:
The Java exception mechanism has a TRY block, a Catch block, a Finally block, and a TRY block must be followed by a Catch block, or a catch block, or both, such as:
Try {}
Catch (exception class E) {}
or
Try {}
Finally {}
or
Try {}
Catch (exception class E) {}
Finally {}
Role: The TRY block is used to pace an abnormality. The Catch is used to handle the exception to the captive, and the code in the Finally block is performed whether there is any exception to execute, usually used to release some key resources, such as database connections.
Operation order:
A. If there is no abnormality, the program sequence is executed, after executing the TRY block (whether it should not return) If there is a finally block. Note that the program has two exits, one in the Catch block, one in the finally block, as shown in the following example:
A1: Export in the TRY block
First modify the print () function of the C1 class make it no exception
Public Final Void Print () throws a {
Try {
// throw new a ();
}
Finally {
// system.out.println ("c1.finally");
// Return;
}
}
Rewind test function:
INT test () throws a {
Try {
(New C1 ()). Print (); // does not abnormally
Return 1;
}
Catch (a e) {
System.out.println ("Catch11111111");
Return 2;
}
Finally {
System.out.Println ("Finally1111111111");
}
}
Test Results:
Finally111111111111
1
Main.try
Analysis: When the program executes the "return 1;" statement in the TRY block, in order to ensure the implementation of the FINALLY block, the function test () has a finally block after returning to the TRY block, if there is execution, then return it back .
A2. Export in Finally block:
Modify the test () function code, plus the return statement in the last line of the Finally block:
INT test () throws a {
Try {
(New C1 ()). Print (); // does not abnormally
Return 1;
}
Catch (a e) {
System.out.println ("Catch11111111");
Return 2;
}
Finally {
System.out.Println ("Finally1111111111");
Return 3;
}
}
The result of the execution:
Finally111111111111
3
Main.try
Cause: When the program is executed normally into the Return 2 in the TRY block; the Finally block is then executed, and since there is a return command return 3 in the Finally block; so the program returns in advance.
Return to the directive in the Finally block:
The Finally block is originally used to release some key resources, and if the return instruction affects the normal operation of the Java exception mechanism.
Correct
Try {}
Catch (exception class E) {}
Finally {}
Such an abnormally caught mechanism, the impact is not very obvious. E.g:
First modify the print () method of the C1 class makes it possible to throw an exception:
Public Final Void Print () throws a {
Try {
Throw new a ();
}
Finally {
// system.out.println ("c3.finally");
// Return;
}
}
The test function is unchanged, the results are as follows:
Catch1111111111
Finally111111111111
3
Main.try
There is no big effect on Return 2 from the Catch block; there is no big effect.
But for
Try {}
Finally {}
Such an abnormal pocket mechanism is large, because the TRY block does not have the corresponding CATCH block passion, so when the abnormality occurs, the running environment is looking forward to the appropriate CATCH block processing exception, but at the superior Catch block processing Before an exception, execute the finally block first. If there is a return instruction in the finally block, the program will return it normally, then continue, that is, the last level of Catch block does not have the opportunity to handle exceptions. E.g:
The print () method of rewriting C1 is as follows:
Public Final Void Print () throws a {
Try {
Throw new a ();
}
Finally {
/System.out.println ("c1.final ");
Return;
}
}
Test () test function constant:
C1.FinalLinally
Finally111111111111
3
Main.try
(Note no output catch1111111111)
Although the TRY block of the Print () function throws an abnormality, the CATCH block of the upper Test () function has no chance to handle an exception, because the Return's RETURN command changes in the print () function changes.
The same situation also appears in the following:
Change the print () function of C1:
Public Final Void Print () throws a {
Try {
Throw new a ();
}
Finally {
//System.out.println ("c3.finally ");
// Return;
}
Change the Test () function:
INT test () throws a {try {
(New C1 ()). Print ();
}
Catch (a e) {
System.out.println ("Catch11111111");
Try {
(New C1 ()). Print ();
Return 0;
}
Finally {
System.out.println ("Finally222222222");
Return 2;
}
}
Finally {
System.out.Println ("Finally1111111111");
Return 3;
}
}
Note: Catch, Finally block is nestled in the Catch block.
Output results:
Catch1111111111
Finally222222222
Finally111111111111
3
Main.try
Note: The return value of the output is 3, and the Catch block in the main function is not working.
This is also another unusual loss,
Thinking in java speaks unusual loss:
Using
Try {}
Finally {}
The processing exception is that if the TRY block is abnormal, there is an abnormality when the code in the FINALLY block is executed before the Catch block of the previous level is handled. The exception in the TRY block will not process, and the FINALLY block appears. The abnormality, that is, the abnormality in the TRY block is lost.