. 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.
Private void THREADPROCESSCB ()
{
Try
{
...
}
Catch (Exception ee)
{
This.begininvoke (New Workerthreadexceptionhandlerdelegate (Workerthreadexceptionhandler), New Object[]{ee});
}
}
Exception Office the best side of the reason Method
No:
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.
Needs:
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
http://www.microsoft.com/downloads/details.aspx?FamilyID=8ca8eb6e-6f4a-43df-adeb-8f22ca173e02&DisplayLang=en
Compiling engineering
Add a reference to a new project
Introducing Namespace Microsoft.ApplicationBlock.ExceptionManagement