2005.1.11 ou yanliang
Course Introduction
Let's discuss how to handle exceptions in. NET and see what is the best way to handle exceptions.
Basic Content
Understand how to program using Microsoft. NET Framework
VS. NET Usage
C # Or VB. NET
Course Content
Design Exception Handling
Basic Exception Handling syntax in. NET
Exception priority
How to throw an exception
Best Exception Handling Method
Application Block for Exception Handling)
Exceptions may occur.
Most software systems are not totally reliable!
It is necessary to compile the exception handling program from the perspective that exceptions may occur to cope with possible errors of the program.
Create a good Exception Handling Policy.
Basic Exception Handling syntax
All exceptions are derived from exceptions.
Demonstration 1
Exception Handling
Manufacturing an exception except 0
Catch Block priority
When multiple Catch blocks exist, the Catch block can filter exceptions. We should first intercept specific exceptions and then intercept general exceptions. When writing an exception capture, the sequence of caught exceptions is very important. If an exception has been caught by a Catch, the subsequent Catch will no longer process the exception.
Throw an exception
An exception is thrown to notify the program caller that the program produces an error because of their call.
For example, an interface that cannot be implemented may throw a "NotSupportedException" exception.
General Framework exception: A "NotImplementedException" exception may be thrown during development.
You can also derive your own specific exceptions from system exceptions, which are used by specific applications.
Generally, we recommend that you derive from the System. ApplicationException class when deriving your own exceptions. This refers to a System-level exception. Therefore, we should follow a rule when writing an application.
Syntax for throwing an exception
Demonstration 2
Throw an exception
Add a Catch Block to capture exceptions
Running result
If we throw an Exception, we 'd better throw a specific Exception instead of only throwing an Exception of the Exception type. In this way, we can capture exceptions of the corresponding type.
Handle unexpected exceptions
Make sure that all program entries use try-catch.
All exceptions are intercepted in catch.
Exception Handling Technology
1. Record exceptions
An exception is recorded in the file.
An exception is recorded in the database.
An exception is recorded in Eventlog.
2. An error occurred while sending an email notification.
3. If an exception occurs, user-friendly notifications are sent to the user.
Demonstration 3
Handle unexpected exceptions
An unauthorized access exception occurs. The second button is selected by default.
IO exception
A basic Exception. To catch a basic Exception, you can only write catch without writing the following Exception content. This can also capture all exceptions, but it cannot know the exception details.
Finally, restore with the mouse pointer
Running result
Add a global exception handling function
Although we did not write a try-catch Block to catch exceptions in all application portals, however, you can use the ThreadException attribute in the Application object to set a delegate to capture exceptions in all unprocessed Main UI threads.
This method can only handle exceptions in the main thread. Exceptions cannot be captured by other working threads or auxiliary threads.
Demonstration 4
Global Exception Handling
ServiceNotification indicates that the current dialog box is placed at the forefront of all programs.
Here we directly throw an exception and do not use try-catch to wrap it.
In this case, global exception handling intercepts this exception.
Exception in Worker Threads
When writing multi-threaded code, you must consider exceptions in the work thread.
Use try-catch at the thread entry
Use delegate or other methods to notify the main thread of exceptions
Demonstration 5
Exceptions in the worker thread
Create a thread
If an exception is thrown in a non-main thread, BeginInvoke is used to notify the main thread.
During multi-thread interoperability, if our working thread wants to access the interface elements in the main thread, We must access it through BeginInvoke. That is to say,Which thread must access the interface element of the thread?. We must call the BeginInvoke method in the corresponding interface element thread to access its own interface element.
Execute EmulateLongProcess in another thread. If an exception occurs in this method, the catch will catch this exception, which calls the BeginInvoke method. The BeginInvoke method here must be this. BeginInvoke. This must be the current frmCustomer.
FrmCustomer completes the inininvoke method and allows the current form to execute the WorkerThreadExceptionHandler method. We use a delegate Method for execution.
Here, the WorkerThreadExceptionHandler calls a main thread exception delegate and throws the exception to the main thread.
We cannot directly call the WorkerThreadExceptionHandler method in the catch block, because this method needs to access the interface elements of the main thread. Therefore, we must use the BeginInvoke method to allow the main thread to call this method by itself.
Of course, we can use this. BeginInvoke instead of this. TextBox1.BeginInvoke. Because the most fundamental meaning of BeginInvoke here is to let the main thread complete this method.
Running results, exceptions are caught correctly
Here, if we remove the try-catch Block of the function called by the working thread and throw an exception in it
Running results, exceptions must have occurred, and the interface program does not respond, so the user will be surprised.
That is to say, if the worker thread does not handle exceptions, the exception will disappear and no one will know what the exception is.
The best way to handle exceptions
Do not:
Catch exception and re-throw
Because a new exception is thrown, some messages are lost. For example, some call stack information contained in this exception is lost.
Throws an exception to control code execution.
Add the try-catch Method/attribute/structure at the entrance of the constructor of the program.
MessageBox. Show (MyException. ToString ())
Try-catch is used, but no exception is handled.
Required:
Remember the exception handling policies from beginning to end
Use try-catch: event processing function, main function, and thread entry at all entrances of the application.
Handle all unexpected exceptions
When writing code, consider the worst case of the application.
Displays good information and provides appropriate administrator contact information.
Provide possible options when possible (termination, retry, ignore)
Exception Handling Block
Publisher/subscriber Design Mode
Download the exception handling Block
Http://www.microsoft.com/downloads/details.aspx? FamilyID = 8ca8eb6e-6f4a-43df-adeb-8f22ca173e02 & DisplayLang = en
Compile the project
Add reference in new project
Introduce the namespace Microsoft. ApplicationBlock. ExceptionManagement
Release exceptions using ExceptionManager. Publish ()
Configure the app. config file to enable Exception management
You can add your own exception handling module by configuring some information in the. config file.
Configure the exception handling Application Block
Demonstration 6
Exception Handling Block
First, add two existing exception projects provided by Microsoft and add references.
Then, place the ExceptionManager Publish method in the main thread exception and input the exception information. In this way, we can Publish the exception information and write the published location in the config file.
The configuration file first defines a configuration area section named predictionmanagement. The following is an exceptionManagement configuration block. Its Attribute mode is set to on, indicating that all the publisher configuration functions are enabled. Logname is the name of the log in log management. Applicationname is the name of the application recorded in the log.
When an exception occurs, we also announce the exception.
Open the Event Viewer of Administrative Tools
We can see this exception log
Summary
Accept the reality: Exceptions will certainly happen
Customize An Exception Handling Policy
You cannot handle exceptions blindly.
Structured exception handling in. NET is a powerful and effective tool.
2010.10.10