C#try Catch block

Source: Internet
Author: User
Tags try catch

Try: The catch block appears for exception handling.

The format is: try{... Code that could have an exception ...}

catch{... Handling of exceptions ...}

finaly{... Code that will execute anyway:}

The above is just the general format, and the following format is present.

1. One try, one or more catch.

2. One try, one or more catch, one finally

3. A try, a finally.

The return statement is not allowed in the finaly block. Error, control cannot leave the finally clause body.

A personal understanding should be given to a catch block (executed at the time of the error) and there may be a return statement, and if

The catch block appears return so the statement inside the finally block executes before the Retun executes. If the finaly block

The return statement within the CATCH block will not be executed. The same statement within a try block (no exception will not execute the CATCH block). I guess that's what it means.

Because the try is born to catch an exception, the exception is caught in the catch block and is captured by the exception class, so the exception class inherits from exception.

So you can catch all the exceptions through exception, or you can capture the exceptions by multiple catch to prevent the missing exceptions.

And the catch is sequential, so you should catch all the exceptions at the end.

You can also throw an exception by throwing up, and the reason for throwing up an exception is to throw an exception to the caller.

This is available at call time using try: Catch to catch the exception.

This exception can be custom content throw new Exception ("error has occurred");

You can also write the throw directly, throw the system exception. This is a simple example.

C#try Catch block

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.