Exceptions and exceptional handling

Exceptions:
What are exceptions you may ask,exceptions are flaws in the programs that are to be taken care of.They disturb the normal flow of the program and creates serious problems if not handled properly.They can be run-time or compile time or you may say unchecked or checked exceptions.But what are errors then,exceptions that cannot be recovered  are called errors.For example:Programmer has declared an array of objects of string class of size n,but has inserted greater than n value,its an array-index-out-of-bound exception,or if called the method of a class that neither exist on the package.There are lots of predefined exceptions in java including them. 
There are basically two types of exceptions as I said,checked and unchecked exceptions.

  • Checked exceptions are notified while compiling the program,i.e. the compiler checks that it would cause further problem,that's why they are called checked exceptions.Examples are:IOExeption,sqlException.
  • Unchecked exceptions are non-recoverable and compiler does not know whether they are going to occur or not and does not care if they occur and don't check them.There might be a situation when file provided in the code in not even there or the server is down for site,thus these situation lead to runtime exceptions.Examples are:Arithmetic and nullpointerException(Heard of it??).
What to do if exceptions are residing to attack in our program,you never know...So why take the risk??Similiar to C,C++ Java also has exceptional handling mechanism that includes three steps:
  1. Targeting the area of code where exception can occur(Risky area).
  2. Throwing the risk to handle it to the catcher to handle it(So the exception has finally occured we must do something about it,this part is optional BTW).You have to create a separate class which must extend exception class to have your personal exception solving technique.
  3. Handling the exception at proper way or at least notify the user the area where the exception has occured(this the catch).
Syntax:
try    //here is the risk
{
//if something went wrong 
}
catch(Exception-name ex)//name to exception to recover eg.Arithmetic,FileNotFound,IoException Blah Blah
{
//code to solve
//or type ex.printStackTrace(); to find where the problem is
}


With throws keyword:
try    //here is the risk
{
//if something went wrong 
//throws new WhattheHellException();//new object of object class has been called to rescue(optional)
}
catch(WhattheHellException ex)
{
//code to solve
//or type ex.printStackTrace(); to find where the problem is
}

Example without 'throws':
try 
{
int a,b,c;
c=a/b;
}
catch(ArithmeticException ex)
{
System.out.println("It is not possible to divide and integer by zero");
}

Example with 'throws':
class CantVoteException extends Exception
{
public CantVoteException()
{
System.out.println("Can't vote");
}
}
//try block in some class
try
{
int age;
System.out.println("Enter your age..");
Scanner sc=new Scanner(System.in);
age=sc.nextInt();
if(age<18)
{
throws new CantVoteException();//now CantVoteException class is called your own created class,cool isn't it??
}
else
{
System.out.println("You can vote now..");
}
}


Things to remember while handling exceptions:

  • A single try block can have multiple catch blocks but reverse is not true.
  • It is not always necessary to handle the the exception if occurred but the reporting where the exception occurred is a must by printStackTrace method. 
  • finally block is optional,but it is important to free memory,close files,save previous works etc.So don't ignore it.
  • throw and throws are different keywords,throw is used to call the exception class within the try block and throws is used while defining methods,its your choice which to use.
  • Every try block must have at-least one catch block,i.e.pair of try-catch is necessary.

Comments

Popular posts from this blog

Four building blocks of Object oriented programming language

Lists in java