Try catch finally has three simple keywords, but the processing process is complicated. Next we will discuss them in various situations, draw a conclusion, and propose suggestions for use.
1. No exceptions
class Program { static void Main(string[] args) { try { //throw new Exception("my exception"); Console.WriteLine("in try"); } catch (Exception ex) { Console.WriteLine("in catch: " + ex.Message); } finally { Console.WriteLine("in finally"); } Console.WriteLine("after finally"); }
Output:
In try
In finally
After finally
Analysis:
Because no exception occurs, the catch will not be captured, so do not execute the code in the catch, execute the finally code, and then execute the subsequent code.
2. An exception occurs and is caught by this level of catch.
class Program { static void Main(string[] args) { try { throw new Exception("my exception"); Console.WriteLine("in try"); } catch (Exception ex) { Console.WriteLine("in catch: " + ex.Message); } finally { Console.WriteLine("in finally"); } Console.WriteLine("after finally"); } }
Execution result:
In catch: My exception
In finally
After finally
Analysis:
When a try block exception occurs, the code will not be executed, but will be caught immediately. Then, the code in the catch block will be executed, the Finally block code will be executed, and the subsequent code will be executed.
3. An exception occurs and is caught by the upper-level catch.
Code:
Class program {static void main (string [] ARGs) {try {test (); console. writeline ("in previous try");} catch (exception ex) {console. writeline ("in catch:" + ex. message);} finally {console. writeline ("in last-level finally");} console. writeline ("After finally");} static void test () {try {Throw new exception ("My exception"); console. writeline ("in try");} catch (ioexception ex) {console. writeline ("in catch:" + ex. message);} finally {console. writeline ("in finally");} console. writeline ("After finally ");}}
Execution result:
In finally
In catch: My exception
In Level 1 finally
After the finally of the upper-level
Analysis:
After an exception occurs, the current level of catch is not caught. The exception continues to flow up. After the catch is caught by the upper level, the finally level is executed, the catch at the upper level is executed, and the finally level is executed.
The code after finally Of the current level will not be executed.
4. An exception occurs. It is not caught by this level of catch, nor is it caught by any higher-level catch.
Code:
Class program {static void main (string [] ARGs) {try {test (); console. writeline ("in previous try");} catch (ioexception ex) {console. writeline ("in catch:" + ex. message);} finally {console. writeline ("in last-level finally");} console. writeline ("After finally");} static void test () {try {Throw new exception ("My exception"); console. writeline ("in try");} catch (ioexception ex) {console. writeline ("in catch:" + ex. message);} finally {console. writeline ("in finally");} console. writeline ("After finally ");}}
Execution result:
Unprocessed exception: system. Exception: My exception
In leleapplication4.program. Test () Location D: \ myprojects \ vs2010 projects \ cons
Oleapplication4 \ consoleapplication4 \ Program. CS: Row 33
In leleapplication4.program. Main (string [] ARGs) Location D: \ myprojects \ vs2010
Projects \ leleapplication4 \ consoleapplication4 \ Program. CS: row number 15
In finally
In Level 1 finally
Analysis:
After an exception occurs, the catch is not captured at the current level. The catch is transferred to the previous level, and the catch is not captured at the previous level. The CLR prints "not handled" until the CLR continues... ", and after the user presses the cancel button, execute the Finally block of the current level, then execute the Finally block of the previous level, and finally force terminate the program.
The content after the Finally block cannot be executed.
5. Conclusion
(1) There is no exception in the try block. Try-> finally-> subsequent code;
(2) In the try block, the code after the exception statement is not executed;
(3) An exception occurs in the try block. If caught by the current catch, the current catch block --> the current Finally block --> subsequent code;
(4) An exception occurs in the try block. If caught by the upper-level catch, the Finally block at the current level --> the upper-level Catch Block --> the upper-level Finally block --> the higher-level subsequent code;
(5) An exception occurs in the try block. If the catch is not captured by any level, the Finally block --> the parent Finally block ---> program is terminated.
6. Suggestions for abnormal use (for reference only)
(1) release programs should try to avoid being forced to close because they do not catch exceptions, so that users may feel that your programs are not robust. Therefore, at least capture all exceptions in the catch at the upper level;
(2) clean up the part of the resource and try to put the Finally block as much as possible, because the Finally block will always be executed in any case.