- Back to Home »
- C#.NET »
- C#.NET Exception Handling
Posted by :
Sudhir Chekuri
Saturday, 9 January 2016
Exception
Exceptions are the runtime errors that leads to abnormal termination of program.Exception handling
It is a mechanism to handle the abnormal termination of program because of runtime errors.We can handle exceptions using try catch block.
Parent of all exceptions is Exception class.
Try block
To use exception handling you must place the code that you want to guard against an exception in the try block.try
{
}
Catch block
Code to handle the exceptions has to be written in the catch block.catch
{
}
Finally block
The block of code that has to be executed even exception occurs or not.finally
{
}
Throw block
Used to force an exception.when an exception is raised in try block it is throwed to catch block to handle it.
exceptions are throwed automatically by the compiler.
user defined throws can also be used to throw an exception.
Simple try catch block example program
Program
using System;
namespace exception
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
try
{
x = 5;
y = 0;
z = x / y;
Console.WriteLine(z);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
Example program for displaying userdefined message in catch block
Program
using System;
namespace exception
{
class Program
{
static void Main(string[] args)
{
int x, y;
try
{
x = 5;
y = 0;
if (y < 1)
{
throw new ApplicationException("error");
x = x / y;
} Console.WriteLine(x);
}
catch (Exception ex)
{
Console.WriteLine("this is an error
message");
}
finally
{
Console.WriteLine("definetly executed");
Console.ReadKey();
}
}
}
}
Program
using System;
namespace exception
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
try
{
x = 5;
y = 0;
z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
Example program to use Multiple catch blocks
Program
using System;
namespace exception
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
try
{
x = 5;
y = 0;
z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
Program
using System;
namespace exception
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
try
{
x = 5;
y = 0;
z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("definetly executed");
Console.ReadKey();
}
}
}
}
Program
using System;
namespace exception
{
class Program
{
static void Main(string[] args)
{
int x, y;
try
{
x = 5;
y = 0;
if (y < 1)
{
throw new ApplicationException("error");
x = x / y;
} Console.WriteLine(x);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("definetly executed");
Console.ReadKey();
}
}
}
}