C # Try-catch-return

Source: Internet
Author: User
Tags finally block

Executing a try before executing a statement in Finally,catch may affect the execution of finally

Usefinally block, you can clean up any resources allocated in the try, and you can run the code even if an exception occurs in the try block. Typically, the control leavesAfter the try statement,finally The statement will block the run.  break, continue, goto, or return statement, or of propagation of an exception out of the try statement. "Data-guid=" 20ea8da6dec7e0a184d6e06697d4594b "> normal execution in  break, Continue, goto  or  < span class= "input font-color" >return  statement execution, or   The propagation of an external exception in the try  statement may cause a control conversion to occur.

The handled exception will ensure that the associatedfinally block. However, if the exception is not handled, the execution of the finally block depends on how the exception unwind operation is triggered. This action also depends on how the computer is set up. For more information, see unhandled Exception processing in the CLR (unhandled exception handling in the CLR).

Typically, when an unhandled exception aborts an application, theIt does not matter whether the finally block is running. However, if you have aThe statements in a finally block must run in that environment, a solution is toThe catch block is added to thetry-The finally statement.Alternatively, you can capture the one that may be above the call stacktry-of the Finally statementThe exception that is thrown in the try block.try-finally statement, or in the Method that is calls that method, or an any method in the call stack. "Data-guid=" 91e6e938ad929c777060761090142e2e "> so that the package can be captured Includes  try-finally block depends on whether the operating system chooses to trigger an exception unwind operation . "Data-guid=" 2dd2b088c7abcc5f71bc00b7236b219c "> If no exception is caught,  finally  block depends on whether the operating system chooses to trigger an exception expansion operation.

try-catch-return Order of executionWhether in a try or in a catch, as long as they have a return and the bottom of finally exist at the same time, in the program run finally first to execute finally, and then execute Return,try-catch-return share a local domain (variable in which to remain consistent)
       static void Main ()        {            Console.WriteLine (Test ());            Console.readkey ();        }           static int Test ()        {            int num = 0;            Try            {                num = 1;                throw new Exception ("Manual control throws Exception");                return num;            }            catch (Exception e)            {                Console.WriteLine (e.message);                return num;              }            Finally            {                num++;                Console.WriteLine ("I'm in finally num={0}", num);            }        }

C # Try-catch-return

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.