System.Exception is the base class for any other Exception classes in the .NET Framework. The Exception class is normally thrown automatically by the CLR during code execution and use of the try and catch constructs in the coding promotes good exception handling. You can also create and throw your own Exception class (it must derive from System.Exception). The below shows how you throw and catch an Exception class in a try and catch block:
The above example throws a basic Exception object (note the new keyword creates one on the heap) and the catch block creates a stack reference (the ex variable) to the object to allow us to access the Exception object members. The most commonly used member, Exception.Message is a text message that explains what issue has occurred.
You may of be aware of the term, ‘Unhandled Exception’. These are essentially exceptions that the CLR has raised that have not been caught, or handed in the application that is executing. The message normally describes the issue and informs you of the Exception type being thrown, for example a ViewStateException, CmdLineException or an ApplicationException, all of which are derived types from the base type System.Exception.
As described above when an Exception object (or any derived Exception object) is caught, we create a variable and access the members of the Exception object. The most commonly used members include:
There are hundreds of Exceptions in the .Net BCL so trying to attempt to learn them all is a no go. If you understand the basic members of this base class however and the additional members added with the all important SystemException and ApplicationException classes, you will be in a good place to start handling exceptions in your applications.