Sunday, March 9, 2014

Exception Handling & Exception Class

Exception Handling

  • 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, are 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.
  • Purpose of Exception  Handling:  To  avoid  "abnormal  application  termination",  even though exception occurs.
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.If the application was terminated abnormally, if will be most inconvenience for the user. So being a programmer, you are responsible to avoid that kind of abnormal application termination, even though exception is occurred at run time
Program of Exception Demo
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 .

exception handling
/*Syntax Of Exception
------------------------------*/
try
    {
       
    }   

Catch (Exception ex)
    {
       
    }

Finally
   {
       
    }
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.
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 for the exception object.
  • Library: System.Exception
finally block:
  • This  block  will  be  executed  automatically  and compulsorily,  after  executing  try  block  /  catch block.
  • That  means  even  though  exception  is  raised  or not raised, the finally block will be executed without fail.
  • This  is  optional  block.  You  can  write  the  exception  handling syntax  only  with try and catch blocks, without finally block.
PROGRAM OF EXCEPTION HANDLIGN
                                                      
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.");
    }
   finally
    {
     Console.WriteLine("This is 'finally' block.");
     Console.Read();
    }
  }
}
}
Types of Catch Block Messages:

Already we have discussed that the catch block generates an error message, when an exception occurs. That error message can be of two types.

  1. User Defined Message
  2. System Defined Message
  1. User Defined Message: Your own message can be written.
    • Error Occurred.
    • Operation is not successful. etc.
  2. System Defined Message: The system provides the description of the error, so that you can print that on the output directly. To access the system defined message, you can use the exception object as follows:
    • Syn:ex.Message

Example: