Exception Handling in C#
Exception Handling in C#:
An exception is an erroneous situation that occurs during program execution. Exceptional situation arise when an operation cannot be completed normally. When an exception occurs in an application, the system throws an error. The error is handled through the process of exception handling.
Exception handling is a way to handle errors at runtime that cause application termination.
.NET supports structured exception handling with the help of try, catch, finally and throw key words.The try block
The code, which seems to generate an exception, is placed under the try block. When an exception occurs in the try section, code compilation is transferred to the catch section.
try
{
//Code that may cause an exception
}
A try block should have at least one catch or finally block. We can use both catch and finally block after the try block.
Remember there should be no other code between these blocks.The catch block
In this block you implement methods for dealing with any possible errors of the try block. At the end of this block execution continues to the last code block.
When an exception occurs in the try section, code compilation is transferred to the catch block.
try
{
//Code that may cause an exception
}
catch (...)
{
//Code for handling errors
}
The catch block should be immediately placed after try block.
The catch statement of the catch block takes an object of the exception class as a parameter, which refers to the raised exception. When the exception is caught, the statements within the catch blocks are executed.
One of the properties of the Exception class is called Message. This property contains a string that describes the type of error that occurred. You can then access this Exception.Message property to display an error message if you want. The finally block
The finally block is executed whether or not an exception was thrown.
try
{
//Code that may cause an exception
}
catch (...)
{
//Code for handling exception
}
finally
{
//clean up code
}
The catch block is used to handle exceptions that occur in a try block. The finally block is used to guarantee the execution of statements, regardless of whether an exception has occurred or not.Throwing an Exception
Exceptions can be explicitly generated by a program using the throw keyword.
Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then thrown with the throw keyword. The runtime then searches for the most compatible exception handler.
using System;
namespace exception_example1
{
class Program
{
static void Main(string[] args)
{
int i, k = 0, j;
try
{
Console.Write("Enter a number one: ");
i = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a number two: ");
j = Convert.ToInt32(Console.ReadLine());
k = i / j;
}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
finally
{
Console.WriteLine("Output of division : {0}", k);
Console.ReadKey();
}
}
}
}
An Exception is an object delivered by the Exception class. This Exception class is exposed by the System.Exception namespace.Exceptions in C# provide a structured, uniform, and type-safe way of handling both system-level and application-level error conditions. Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally and throw.Example of code snippet as below