Professional Documents
Culture Documents
An exception is a condition that is caused by runtime error in the program .When the c#
compiler encounter an error such as dividing an integer by zero, it create an exception
object and throw it(i.e. inform us that an error has occurred).
If the exception object is not caught and handled properly the compiler will
display an error message and will terminate the program.If you want the program to
continue with the execution of remaining code then we should try to catch the exception
object thrown by error condition and then display an appropriate message for taking
corrective actions.This task is known as exception handling.The error handling code
basically consist of two segment.
1) To detect error to throw exception.
2) Catch exception and take appropriate action.
namespace demo_try
{
class Program
{
static void Main(string[] args)
{
int a, b, c, d;
a = 45;
b = 5;
c = 5;
try
{
d = (a / (b - c));
}
catch (DivideByZeroException e)
{
namespace demo_mutiple_catch
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Enter the 2 numbers");
try
}
}
}
It is possible to have more than one statements in the try block then there is possible to
generate multiple exception.
try
{
Statements;
}
Catch(Exception type1)
{
Statements;
}
Catch(Exception type2)
{
Statements;
}
Catch(Exception type n )
{
Statements;
}
When an exception in a try block is generated the c# treat the multiple catch statement
like cases in switch statements. The first statements whose parameter matches with
exception object will be executed and the remaining statements will be skipped.
Nested ‘try’ block
C# permit us nested try block inside program.
When nested try block are executed the exception that are thrown at various points are
handled as follows.
1) The point p1 are outside the inner try block.since any exception thrown at these
point will be handled by catch in outer block.The inner block simple ignored.
namespace demo_nested_try
{
class Program
{
Throw
It is possible to throw an exception manually by using throw statement. You can throw a
new exception nearly anywhere in c# code.It’s general form is
throw exception_object;
where exception object must be an object of an exception class derived from ‘Exception’.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace demo_throw
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
a = 45;
b = 5;
try
{
c = a / b;
Console.WriteLine("The value of c is" + c);
throw new DivideByZeroException();
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("End");
Console.ReadKey();
}
}
}
Custom Exception
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo_cust_exce
{
class myexp: Exception
{
public myexp(string txt) :base(txt)
{
}
}
class demo
{
public void show()
{
int a, b, c;
a = 45;
b =5;
}
}
class Program
{
static void Main(string[] args)
{
demo d = new demo();
d.show();
Console.ReadKey();
}
}
}
Finally
The purpose of finally statement is to ensure that the necessary cleanup of Object, usually
objects that are holding external resource happens immediately even if an exception is
thrown. One example of such cleanup is calling close a file (File stream) immediately after
use instead of waiting for the object to be garbage collector by CLR.
A database connection is another good example for being closed in finally
block. Because the number of connection allowed to database server is sometimes limited, it
is important to close database connection quickly as possible. If an exception is thrown
before you can close your connection this is another case where using the finally block is
preferable to waiting for garbage Collection.
In this example there is one invalid conversion statement that causes an exception
but the finally clause will executed and display the outputs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Exception Hierarchy
In C#, all the exceptions are derived from the base class Exception which gets further
divided into two branches as ApplicationException and another one
Different Exception Classes: There are different kinds of exceptions which can be
generated in C# program:
Divide By Zero exception: It occurs when the user attempts to divide by zero
Out of Memory exceptions: It occurs when then the program tries to use excessive
memory
Index out of bound Exception: Accessing the array element or index which is not
present in it.
Stackoverflow Exception: Mainly caused due to infinite recursion process
Null Reference Exception : Occurs when the user attempts to reference an object
which is of NULL type.
…..and many more.
Exception Chain
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
one();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadKey();
}
public static void one()
{
try
{
two();
}
catch (Exception e)
{
throw new Exception("First Exception", e);
}
}
ArgumentOutOfRangeException Raised when the value of an argument is outside the range of valid
values.
FileNotFoundException Raised when a physical file does not exist at the specified location.
IndexOutOfRangeException Raised when an array index is outside the lower or upper bounds
of an array or collection.
OutOfMemoryException Raised when a program does not get enough memory to execute
the code.