What Is try-catch-finally in C#
As mentioned above try catch finally is an error handling method in which the code that might give an error resulting in interruption is executed in try block and in case of error there is an instruction given to the catch block that will execute in response to that respective error. At last, the finally block is executed to clean up resources, regardless of either any error had been encountered or not and below is the syntax for using this method:
// Code that might give error
}
catch (errorhandlingType1 ex) {
// instruction to execute in case or error1
}
catch (ErrorhandlingType2 ex) {
// instruction to execute in case or error2
}
finally {
// code to conclude all the process
}
In the try block, the code that is present in it may give an error and if an error occurs, the program goes to the corresponding catch block and the catch block handles it according to the instruction given inside it. If there are more than one catch blocks, then each one catches a different type of exception.
The finally block contains the instruction that will always be executed, although whether any error is encountered or not. To further illustrate I have given an example C# code that will make it easy for everyone to understand the concept:
class Program {
static void Main(string[] args) {
try {
int a = 10;
int b = 0;
int result = a / b;
Console.WriteLine("The result is: {0}", result);
}
catch (DivideByZeroException e) {
Console.WriteLine("Cannot divide by zero.", e);
}
finally {
Console.WriteLine("End of program.");
}
}
}
In this code, the try block contains the code that may give an error as in this case, we are attempting to divide by zero, which will cause a DivideByZeroException.
The catch block specifies the type of exception to catch (in this case, DivideByZeroException) and what to do when the exception is caught (simply printing an error message).
The finally block contains the instruction that will always be executed, although whether any error is encountered or not. This is useful for performing any cleanup or finalization tasks, such as closing a file or database connection.
When we run this code, it will output “Cannot divide by zero.” to the console, since the catch block handles the error and it will then print “End of program.” as the finally block is executed.
Similarly, if on case there is no exception or error then the catch function will not operate and to illustrate the concept here is a C# code:
class Program {
static void Main(string[] args) {
try {
int a = 10;
int b = 2;
int result = a / b;
Console.WriteLine("The result is: {0}", result);
}
catch (DivideByZeroException e) {
Console.WriteLine("Cannot divide by zero.", e);
}
finally {
Console.WriteLine("End of program.");
}
}
}
In this code, the try block contains the code that will not throw any exception as in this case, we are attempting to divide by two, which wouldn’t cause any error.
The catch block specifies the type of exception to catch (in this case, DivideByZeroException) and what to do when the exception is caught (simply printing an error message).
The finally block contains the instruction that will always be executed, although whether any error is encountered or not. This is useful for performing any cleanup or finalization tasks, such as closing a file or database connection.
When we run this code, the output of division will be displayed to the console, since there was no exception so the catch block will not be executed and next it will then print “End of program.” as the finally block is executed.
Conclusion
In summary, try-catch-finally is an important control structure in C# that allows developers to gracefully handle exceptions and errors that may occur during program execution. This tutorial is a guide on how to use try-catch-finally in a C# code.