. NET exception handling attention point __.net

Source: Internet
Author: User
Tags exception handling

. NET exception handling Attention points

time to throw an exception:

When designing a function, you should use the code to avoid known exceptions as much as possible, such as: null values, except 0. However, if an unknown exception is encountered and the return value of the function is not sufficient to express the encountered exception, this exception should be thrown and, when necessary, should be designed with its own exception class, using InnerException to express the unknown exception encountered.

Worm, because a function can throw an exception, you should use the <exception cref= "exception" to mark the function annotation when you add it. When you use a function, you can see the tag by looking at the note to avoid known exceptions.

Office Reason not know different often:

Make sure all the program entrances are try-catch.

Intercepts all exceptions in the catch and handles them. The exceptions are handled in the following ways:

1. Record exceptions

To record an exception in a file

Logging exceptions in the database

To record an exception in EventLog

2. Send e-mail to notify the exception

3. Notify the user in a friendly (user-friendly) manner when the anomaly arises

Capture UI Unhandled exception in thread: Add a Global exception Office Rationale Letter number

Although we do not write the Try-catch block of the catch exception at all application entrances, using the ThreadException property in the Application object, you can set a delegate to capture the exceptions that occur in all unhandled main UI threads. This method can only handle exceptions in the main thread, and other worker threads and worker threads are not caught in the exception.

Capture work Line Process ( Worker Threads ) in the not processed Exception

When writing multithreaded code, you must consider the exceptions that occur in the worker thread. The entry of the thread uses Try-catch to catch unhandled exceptions that occur in the worker thread, and then notifies the main thread of the exception that occurred in the catch using delegate or other methods.







Catch (Exception ee)


This.begininvoke (New Workerthreadexceptionhandlerdelegate (Workerthreadexceptionhandler), New Object[]{ee});



Exception Office the best side of the reason Method


The catch exception is Re-throw, because a new exception is thrown, which loses some messages, such as loss of some call stacks with the exception.

Control the execution of your code by throwing an exception.

Add the Try-catch method/property/construct at the entrance of the program's constructor.

MessageBox.Show (Myexception.tostring ()).

Try-catch is used but does not handle an exception, which hides the exception, is not conducive to discovering problems, and may cause the program to enter an unknown processing branch.


The policy of exception handling should be remembered from beginning to end.

Throws a specific exception, rather than throwing only the exception type exception. In this way we capture the corresponding type of exception.

Use the Try-catch: event handler function, main function, thread entry at all entry points in the application.

Handle all unexpected exceptions in your code.

Be careful when writing code to take into account the worst-case scenario for your application.

Show good information and provide appropriate administrator contact information

Provide possible choices (terminate, retry, ignore) if possible

Exception Office procedure Block

Publisher/subscriber design Pattern

To download an exception handler block


Compiling engineering

Add a reference to a new project

Introducing Namespace Microsoft.ApplicationBlock.ExceptionManagement

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.