Exception Handling in C#
It is very common for software applications to get errors and exceptions when executing code. If these errors are not handled properly, the application may crash and you may not know the root cause of the problem. Exception handling is the method of catching and recording these errors in code so you can fix them. Usually, errors and exceptions are stored in log files or databases.
In C#, the exception handling method is implemented using the try-catch and finally statement. In this article, learn how to implement exception handling in C#.
Try, catch, and finally in C#
The try, catch, and finally statement in C# implements exception handling. The try encloses the code that might throw an exception, whereas the catch handles an exception if one exists. The final is used for any cleanup work that needs to be done.
Try catch finally block syntax.
try {
// Statement which can cause an exception.
} catch (Type x) {
// Statements for handling the exception
} finally {
// Any cleanup code
}
If any exception occurs inside the try block, the control transfers to the appropriate catch block and later to the final block.
But in C#, both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a final block or with both catch and finally blocks.
If there is no exception occurring inside the try block, the control directly transfers to the final block. We can say that the statements inside the final block are always executed. Note that it is an error to transfer control out of a final block by using break, continue, return, or goto.
Here is a detailed article on C# Try Catch Statement.
Uncaught Exceptions in C#
Let's learn this by example. The following program will compile but will show an error during execution. The division by zero is a runtime anomaly, and the program terminates with an error message. Any uncaught exceptions in the current context propagate to a higher context and look for an appropriate catch block to handle it. If it can't find suitable catch blocks, the default mechanism of the .NET runtime will terminate the execution of the entire program.
//C#: Exception Handling
//Author: [email protected]
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 100/x;
Console.WriteLine(div);
}
}
The modified form of the above program with an exception-handling mechanism is as follows. Here we are using the object of the standard exception class DivideByZeroException to handle the exception caused by division by zero.
//C#: Exception Handling
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100 / x;
Console.WriteLine("This linein not executed");
}
catch (DivideByZeroException)
{
Console.WriteLine("Exception occured");
}
Console.WriteLine($"Result is {div}");
}
}
The result from the above code is shown below.
In the above case, the program does not terminate unexpectedly. Instead, the program control passes from where the exception occurred inside the try block to the catch blocks. If it finds any suitable catch block, it executes the statements inside that catch and continues with the normal execution of the program statements.
If a final block is present, the code inside the final block will also be executed.
//C#: Exception Handling
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100/x;
Console.WriteLine("Not executed line");
}
catch(DivideByZeroException)
{
Console.WriteLine("Exception occured");
}
finally
{
Console.WriteLine("Finally Block");
}
Console.WriteLine($"Result is {div}");
}
}
Remember that in C#, the catch block is optional. The following program is perfectly legal in C#.
//C#: Exception Handling
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100/x;
Console.WriteLine("Not executed line");
}
finally
{
Console.WriteLine("Finally Block");
}
Console.WriteLine($"Result is {div}");
}
}
In C#, a try block must be followed by either a catch or a final block. But in this case, since there is no exception handling catch block, the execution will get terminated. But before the termination of the program, statements inside the final block will get executed.

Join the conversation! Your thoughts help the community grow.