When we write programs at ordinary times, we inevitably need to make assumptions about multiple program flows. If the conditions are not met, the program sometimes needs to throw an exception.
We know that exceptions fall into two categories: exception and applicationexception.
Let's take a look at Microsoft's explanation:Exception indicates an error occurred during application execution, which is caused by the common language runtime.
Applicatonexception is an exception that occurs when a non-fatal application error occurs. It is caused by a user program, rather than a common language runtime.
I believe that many people may be confused when they read this explanation. They can't help but raise such questions. What kind of errors are fatal?
For a long time, I have no idea what the difference between the two is. When I write a program, I always throw an exception using exception. In the past few days, I have taken a look at the Community Server (CS). In the CS system, I have customized exception handling classes. This custom exception handling class inherits applicationexception. I read msdn and explained the following:If you want to design an application that requires creating your own exceptions, derive from the applicationexception class. Applicationexception ExtensionExceptionBut no new features are added. This exception is provided as a way to differentiate application-defined exceptions from system-defined exceptions.
I specially created a web project and wrote a short program. One throws an exception and the other throws an applicationexception. It seems that there is no difference. Later, I thought about it carefully. If a fatal error occurs during the program running, I should record it in the system "Event Viewer". With this in mind, I opened the System Event Viewer, as expected, the thrown exception is recorded in the System Event Viewer, And the thrown applicationexception is not recorded. Well, the difference is.
Based on the above description, I have the following conclusions:
If the program has caught exceptions and recorded the exception information, we should throw an applicationexception.
If the program captures and processes exceptions to the CLR, we should throw an exception, so that our program is in the System Event Viewer when the error occurs, you can also view a history record.
In the CS system, the program itself can capture exceptions and handle the exception information accordingly. Its exception handling class can be understood by inheriting applicationexception.