Exception Handling


Errors:
A program mistake is said to be an error.
There are three types of errors:
1)  Compile time errors(Syntax errors)
2)  Runtime errors(Exceptions)
3)  Logical errors

Runtime errors handling mechanism is called as exception handling.

In exception handling we use the following keywords.
1)  try 2) catch 3) throw 4) throws 5) finally

The syntax of try and catch blocks:
try
{
         ============= => Task code
}catch(ExceptionClassName ObjectReference)
{
         ============= => Error Message
}
try block must be associated with at least one catch block or finally block.
All exceptions are classes in Java.
Whenever exception occurs in a Java program, then the related exception class object is created by JVM, passed to exception handler(catch block) and exception handler code is executed.

There are two types of exceptions:
1)  Checked Exceptions
2)  Unchecked Exceptions

Checked Exceptions:
Ø The exception classes  that are derived from java.lang.Exception class are called checked exceptions. Checked exceptions do not include java.lang.RuntimeException class and all its sub classes.
Ø All checked exceptions must be handled explicitly otherwise compile time error occurs.
Ø The Java compiler checks for try & catch blocks or throws clause for this kind of exceptions.
Ø All application specific exceptions are comes under this category.

Unchecked Exceptions:
Ø The exception classes that are derived from java.lang.RuntimeException class are called unchecked exceptions.
Ø All unchecked exceptions are handled by system implicitly.
Ø Handling unchecked exceptions are optional by programmer.
Ø Unchecked exceptions are handled by programmer to display
Ø user friendly error messages only.
Ø The Java compiler does not check for try & catch blocks or throws clause for this kind of exceptions.
Ø All general exceptions comes under this category.

Example1:
class Demo
{
         public static void main(String args[])
         {
                 try{
                 int x=Integer.parseInt(args[0]);
                 int y=Integer.parseInt(args[1]);
                 int z=x/y;
                 System.out.println(z);
                 }catch(ArrayIndexOutOfBoundsException ae)
                 {
        System.err.println(“Please pass two arguments”);
}
catch(NumberFormatException ne)
{
System.err.println(“Please pass two numbers only”);
}
catch(ArithmeticException ae)
{
         System.err.println(“Please pass second                                                                     argument except zero”);
}
}
}
Execution:
1)  C:\> java Demo
Please pass two arguments
2)  C:\> java Demo abc xyz
Please pass two numbers only
3)  C:\> java Demo 10 0
Please pass second argument except zero
4)  C:\> java Demo 10 2
5

Example2(Creating checked exception and handling):
class NegativeNumberException extends Exception
{
}
class Demo
{
         void cube(a) throws NegativeNumberException
         {
                 if(a>0)
                    System.out.println(a*a*a);
                 else
                    throw new NegativeNumberException();
}
public static void main(String args[])
{
         try{
         int x=Integer.parseInt(args[0]);
         Demo d=new Demo();
         d.cube(x);
         }catch(NegativeNumberException ne)
         {
         System.err.println(ne);
}
}

}
Example3(Creating unchecked exception):
class NegativeNumberException extends RuntimeException
{
}
class Demo
{
         void cube(a) throws NegativeNumberException
         {
                 if(a>0)
                    System.out.println(a*a*a);
                 else
                    throw new NegativeNumberException();
}
public static void main(String args[])
{
         int x=Integer.parseInt(args[0]);
         Demo d=new Demo();
         d.cube(x);
}
}