This is one of the major features of OOP languages like C++, VC++, VB.NET, C#.NET, Java etc.
Def: "The process of handling the run time exceptions" is called as "Exception handling".
Note: Exception = Run time error
Types of Errors:
1) Compile Time Errors: The errors occurred after compiling the program are called as “compile time errors”.
2) Run Time Errors: The errors occurred during the execution of the program is called as “run time errors”.
Overview of Exception Handling:
The exception may occur at run time, based on the mistake of the user / programmer / system problem also.
When exception is raised, automatically it leads to "abnormal application termination".
The cause of the exception may be anything; the project developer should take care about the exceptions.
As a part of this exception handling, the programmer has to display "particular error message" to the user.
The major purpose of Exception Handling is to avoid "abnormal application termination".
Types of Application Termination:
Normal Application Termination: Whenever the program execution controls executes all the statements in the program and reaches to end of the code, the application will be terminated automatically. It can be called as “Normal Application Termination”.
Abnormal Application Termination: Whenever an exception occurred at run time, the application will be terminated automatically. It can be called as “Abnormal Application Termination”.
Simple Demo on Exceptions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExceptionDemo1
{
class Program
{
static void Main(string[] args)
{
string[] Cities = { "Vijayawada", "New Delhi", "Banglore", "Hyderabad" };
Console.WriteLine(Cities[3]);
Console.WriteLine(Cities[4]);
Console.Read();
}
}
}
In the above code, the highlighted line contains an error, because it is trying to access an array element which is in out of range of the array. So, it leads to abnormal application termination at run time. To avoid this, we have to implement exception handling for this code.
Syntax of Exception Handling:
try
{
---------------------;
---------------------;
---------------------;
---------------------;
}
catch (Exception ex)
{
--------------------;
--------------------;
}
In the above syntax, we can observe to blocks.
1) try block
2) catch block
1) try block:
• The try block contains the actual code, which is to be executed.
• After every try block, there should catch block without fail.
• The system tries to execute the code in the try block.
• During the execution, if any exception occurs, then the execution control automatically goes to catch block.
• At the same time, the “try” block throws the exception to the catch block in the form of an object. That object is called as “exception object”.
try
{
---------------------;
---------------------;
---------------------;
---------------------;
}
2) catch block:
• This is also known as “error handler”.
• This is followed by the try block.
• The catch block will be executed if any exception is occurred during the execution of try block.
• The catch block contains necessary code which displays an error message to the user.
• This receives the exception, thrown by the try block, in the form of an object. In the following syntax, “ex” is the “Exception object”. The “Exception” is the class of the exception object.
catch (Exception ex)
{
--------------------;
--------------------;
}
Exception Demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExceptionDemo2
{
class Program
{
static void Main(string[] args)
{
try
{
string[] Cities = { "Vijayawada", "New Delhi", "Banglore", "Hyderabad" };
Console.WriteLine(Cities[3]);
Console.WriteLine(Cities[4]);
}
catch (Exception ex)
{
Console.WriteLine("Error occurred.");
}
Console.Read();
}
}
}