Friday, September 10, 2010

Different type of Exception code

//C#: Exception Handling
using System;
class MyClient
{
 public static void Main()
 {
  int x = 0;
  int div = 0;
 try
 {
  div = 100/x;
  Console.WriteLine(“This line in not executed”);
 }
 catch(DivideByZeroException de)
 {
  Console.WriteLine("Exception occured");
 }
 Console.WriteLine("Result is {0}",div);
 }
} 
//C#: Exception Handling
using System;
class MyClient
{
 public static void Main()
 {
  int x = 0;
  int div = 0;
 try
 {
  div = 100/x;
  Console.WriteLine(div);
 }
 catch(DivideByZeroException de)
 {
  Console.WriteLine ("Exception occured");
 }
 finally
 {
  Console.WriteLine("Finally Block");
 }
 Console.WriteLine("the Result is {0}";
}  
 
//This for File not found 
try
{
}
catch(IOException ex)
{
if(ex is FileNotFoundException)
   
}

// This for IO exception
catch(FileNotFoundException ex)
{
}
catch(IOException ex)
{
}

// This for DividebyZero exception
Try
{
}
catch(DivideByZeroException ex)
{
}

// This for SQL exception
Try
{
}
catch(SqlException ex)
{
}