Sunday, March 9, 2014

Exception Class


Exception Class
  • Already you know that C# recognizes the exception as an object.
  • To  declare  the  exception  object,  we  have  used  a  class  called  System.Exception,  in the previous examples.
  • The Exception class recognizes any type of exceptions. In order to catch the particular type of error, C# provides other exceptional classes. Some of them are given here.

Exception Class Description
System.OverflowException Occurs when a large value is assigned to a variable, which is not fit in that variable.
System.FormatException / System.InvalidCastException Occurs when the casting is failed from one data type of another data type.
System.DivideByZeroException Occurs when any number is divided by 0.
System.IndexOutOfRangeException Occurs when an index is accessed in out of range.
System.InsufficientMemoryException Occurs when there is no sufficient memory in RAM for the execution of the application.
System.IO.FileNotFoundException Occurs when a non-existing file is accessed.
System.IO.DirectoryNotFoundException Occurs when a non-existing directory is accessed.
System.IO.FileLoadException Occurs when any error occurred during the opening of any file.
System.IO.IOException Occurs when any error occurred during file read or writing.
System.Threading.ThreadInterruptedException Occurs when any error occurred during the execution of the thread.
System.Threading.ThreadStartException Occurs when any error occurred while starting the thread.
System.InvalidOperationException Occurs when any error occurred while opening the database connection.
System.Data.OleDb.OleDbException Occurs when any error occurred while performing query or non-query transactions on OleDb databases.
System.Data.SqlClient.SqlException Occurs when any error occurred while performing query or non-query transactions on SqlServer database.
System.EntryPointNotFoundException Occurs when you try to run the application, without defining any entry point (main() method).
System.InvalidTimeZoneException Occurs when the system has an invalid time zone setting the date & time settings.

PROGRAM OF EXCEPTION CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExceptionDemo3
{
class Program
{
static void Main(string[] args)
{
 try
 {
  int n1, n2, n3;
  Console.WriteLine("Enter first value:");
  n1 = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine("Enter second value:");
  n2 = Convert.ToInt32(Console.ReadLine());
  n3 = n1 / n2;
  Console.WriteLine("Result is: " + n3);
 }
 catch (DivideByZeroException ex)
  {
   Console.WriteLine("Can't divide the number with zero.");
  }
   Console.Read();
  }
 }
}

Note: If you want to handle more than one type of exception for the same try block, then you need to write multiple catch blocks

PROGRAM FOR MULTIPLE CATCH BLOCK
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExceptionDemo4
{
class Program
{
static void Main(string[] args)
{
 try
 {
  int n1, n2, n3;
  Console.WriteLine("Enter first value:");
  n1 = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine("Enter second value:");
  n2 = Convert.ToInt32(Console.ReadLine());
  n3 = n1 / n2;
  Console.WriteLine("Result is: " + n3);
 }
 catch (DivideByZeroException ex)
 {
  Console.WriteLine("This is divide by zero exception.");
 }
 catch (OverflowException ex)
 {
  Console.WriteLine("This is overflow excpetion.");
 }
 catch (FormatException ex)
 {
  Console.WriteLine("This is invalid cast exception.");
 }
  Console.Read();
 }
 }
}