Introduction
The exceptions are anomalies that occur during the execution of a program. Exception handling is a mechanism in .NET framework to detect and handle run time errors. They can be because of user, logic or system errors. If a user (programmer) does not provide a mechanism to handle these anomalies, the .NET runtime environment provides a default mechanism, which terminates the program execution.
In C# there are three keywords Try, Catch, and Finally for handling exceptions.
- In try block statements it might throw an exception whereas catch handles that caused by try block if one exists.
- The finally block is used for doing any clean up process. The statement in finally block always executes.
e.g.
try
{
// this can cause an exception.
}
catch (Type x)
{
// for handling the exception.
}
finally
{
//this will execute.
}
Exception Class
Following are some common exception classes.
- SystemException- This exception means a failed run-time check used as a base class for other.
- AccessException- This exception means failure to access a type member, such as a method or field.
- ArithmeticException- This exception means arithmetic over – or underflow has occurred.
- DivideByZeroException- This exception means an attempt was made to divide by zero.
- FormatException- This exception means the format of an argument is wrong.
- IndexOutOfRangeException- This exception means an array index is out of bounds.
- NotSupportedException- This exception indicates that a method is not implemented by a class.
- NullReferenceException- This exception means attempt to use an unassigned reference.
- OutOfMemoryException- This exception means not enough memory to continue execution.
- StackOverflowException- This exception means a stack has overflow.
Custom Exception in C#
- Although the .NET framework contains all kinds of exception types which are sufficient in most cases, it can make sense to define custom exceptions in your own applications. They can greatly simplify and improve the error handling and thus increase the overall code quality. Generally, for large scale projects (i.e. having multiple modules talks to each other), one should define custom exceptions.
- For Implementing Custom Exception Handling, we need to derive the class CustomException from the system Base Class ApplicationException.
- Any Custom Exception you create needs to derive from the System.Exception class. You can either derive directly from it or use an intermediate exception like SystemException or ApplicationException as base class.



Sujit PrabhakaranPosted Feb 7, 2024, 4:11 PM
Thank you for sharing this! Nowadays no one will share their code for free. You must be an angel :-)
Dharmendra Kumar PanditPosted Aug 12, 2018, 12:06 PM
NIce Article
GaneshPosted Jul 4, 2015, 9:38 AM
Why you are deriving your class from ApplicationException rather than Exception class. It was recommended to use it.
Former memberPosted Sep 12, 2013, 1:41 AM
appexception is no more usefull
Aaron CronjePosted Jan 21, 2012, 7:45 AM
Good Explain......