Overview
- Most predefined .NET exceptions derive from the System.SystemException class.
- All custom exceptions derive from the System.Exception class.
- For most applications, derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value. (From MSDN)
- The try/catch structure will use the first catch which applies. Order catches from most specific to most general in the code.
- Finally will always be executed, whether an exception was thrown or not.
- A try/catch/finally construct has multiple separate scopes, demonstrated below.
Good practice
- End custom exception classes with ‘Exception’.
- Implement the three recommended common constructors, as shown in the example.
Examples
Scopes
int a;
try
{
int b;
// a and b are accessible.
}
catch (Exception e)
{
int c;
// a and c are accessible.
}
finally
{
int d;
// a and d are accessible.
}
Constructors
using System;
public class EmployeeListNotFoundException : Exception
{
public EmployeeListNotFoundException()
{
}
public EmployeeListNotFoundException(string message)
: base(message)
{
}
public EmployeeListNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}
You May Also Like

Exam 70-536 Study Notes
My study notes for Microsoft exam 70-536: .NET types, collections, generics, I/O, serialization and more, with C# examples.

Starting Exam 70-536 Study
After a burnout, I'm putting my own project on hold to study for Microsoft exam 70-536.

Design Patterns - Proxy Pattern
The Proxy Pattern, and the last article in the design patterns series, now that I've finished reading Head First Design Patterns.




