Exception Handling in C# with Example


An exception is a condition that is caused by a run-time error in the program. When the C# compiler encounters an error such as dividing an integer by zero. It creates an exception object and throws.

Exception Handling:

If the exception object isn’t caught and handled properly, the compiler will display an error message and it will terminate the program. If we want the program to continue with the execution of the remaining code. Then we should try to catch the exception object thrown by the error condition and then display an appropriate message for taking corrective actions. This task is known as Exception Handling.

Purpose of Exception Handling:

1. Find the problem (Hit the exception)
2. Inform that an error has occurred (Throw the exception)
3. Receive the error information (Catch the exception)
4. Take corrective actions (Handle the exception)