C # exception,

Source: Internet
Author: User
Tags finally block

C # exception,

Basic Structure

Try {} catch (other exception) {// capture various exceptions. The exception will be handled immediately here} catch (exception) {// All exceptions except the preceding exceptions are caught here, but an exception needs to be thrown again.} finally {// no matter how the above code is executed, the code in this region must be executed, used for cleanup and other actions}

Rules

1. A try statement must have at least one catch or finally. A try statement is not allowed.

2. If no matching catch statement is found at the top of the Call Stack, an unhandled exception will occur.

3. After a matched catch Block is found, all finally statements in the inner layer are executed, that is, all finally statements between the try statement that throws an exception and the catch statement that matches the exception. However, the finally statement matching the catch statement can be executed only after the catch statement is executed.

4. There are generally three methods for handling catch statements.

1) throw the same exception to the top.

2) throw a different exception to the top, usually providing richer exception information

3) handle exceptions. Do not throw exceptions.

5. The code in catch and finally blocks should be very short to avoid exceptions. Even if an exception is thrown in a catch or finally statement, the clr will throw this new exception up, but the exception information in the original try statement will be overwritten by this new exception, generally, it becomes an unprocessed exception.

6. When an exception is thrown up in a catch statement, the use of the "throw" and "throw e" statements is different. The former does not reset the exception start point, and the latter does, reset the start point to the throw statement here.

7. When a catch or finally block is executed, clr does not allow the thread to terminate

8. When designing a class library exception, do not handle the exception by yourself, and throw an exception so that the caller can handle it by himself. If the exception is handled within the class library, it is not transparent and unfriendly to the caller.

9. It is prohibited to catch Exception exceptions and not throw it again. Otherwise, the application will continue to run if it does not know that an error has occurred.

 

Common information fields in the Exception class

Message: Description of the cause of the exception

Source: indicates the name of the Assembly that generated the exception.

StackTrace: contains all methods called before an exception is thrown.

InnerException: if the current exception is thrown when processing an exception, this attribute points to the previous exception.

 

AppDomain FirstChanceException

This event occurs after an exception occurs and the clr searches for all catch blocks. Therefore, you can use this event to monitor the exceptions thrown by the AppDomain and add log records. The usage principles are as follows:

1. This event cannot handle exceptions, but is only used to receive notifications of exceptions.

 

Try... finally statements are omitted in some common operations, and the compiler will eventually convert these omitted statements to try... finally statements.

1. lock statement. The lock is released in finally.

2. using statement. Call the Dispose method in the finally statement.

3. foreach statement: Call the Dispose method of IEnumerator in the finally statement.

4. destructor: Call the Finalize method in the finally statement.

 

Unprocessed exceptions

Class Library developers do not need to think about unhandled exceptions. Only application developers need to care about unhandled exceptions. MS recommends that application developers receive the default CLR policies. That is, when an unhandled exception occurs, windows writes a record to the event log, which can be viewed in the control panel-> Event Viewer-> windows Log-> application.

 

Application. ThreadException AppDomain. CurrentDomain. UnhandledException

1. The former is used to capture the unprocessed exceptions of the ui thread, which is not captured at this time. The former is also unique to the UI thread, and the latter is used to capture non-UI threads for handling exceptions.

2. In the exception of the former, if this event is not implemented, the following interface will pop up, whether you click "continue" or "exit, will not write records to the event log, click the continue button, the program continues, click "exit" to immediately exit the process, will not execute the Application ThreadExit and ApplicationExit events

3. If the former event is implemented and no new exception is thrown, windows will not write a record to the event log until the exception ends.

4. If the former event is implemented and the exception is thrown again in the Implementation Program, the following prompt box will pop up and windows will write a record to the event log, the ThreadExit and ApplicationExit events of the Application are also executed.

 

 

Relationship between Application. ApplicationExit Application. ThreadExit Exception Handling

1. If the program ends normally or the program is forcibly closed using the task manager, both events are triggered.

2. However, if you do not implement an event with an unhandled exception and click the exit button in the unhandled prompt box, these two events will not be executed, which is strange.

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.