Exceptional handling

Let us try to understand the exception handling with a example:

class eh1
public static void main(String as[])
int x=Integer.parseInt(as[0]);
int y=Integer.parseInt(as[1]);
int z=x/y;

Exception in thread "main" java.lang.ArithmeticException: / by zero

you run the above program the following things will be happened: -----------
  1. JVM invokes main methods then program execution will be started then all the statement inside the main will be executed one by one.
  2. If any problem is happened ,then JVM will do the following things :----------
a)      Identifies the problem.
b)      Identifies corresponding java class identifiers.
c)      Create the object of java class identifier.
d)     Throws the objects.
e)      Catch the objects.
f)       Display the suitable message for the problem occurred.
  1. When there is no problem happened in the program then execution will continue and will end successfully
Types of problems: ------------ while running the program, you can get two type of problem: ----
  •  Exception 
  •   Error
All exceptions are subclasses of java.lang.Exception class.
All  Errors  are subclasses of java.lang.Error class.
Both Exception and Error class are the subclass of java.lang.throwable class             
  •   Exceptions are  the problems which you can handle 
  •   Errors  are the problems which you can not  handle
When exception is raised JVM is handling that exception by default and program execution will be terminated automatically. If I want to handle the execution by continue the program execution what I have to do?
Use try and catch blocks
  • If you have try then must have catch and vice versa 
  • You can’t write any statement between try and catch. 
  •   For every try you have a catch  
  •   Exception will occurred any where in program either inside catch also  
  try {                                                                                                                        
   Catch(Exception e)   // ArithmeticException ae
                         //message             // executed whenever Execution will occurred

Finally Block :--- if you want to execute some statement without fail always then placed those statement inside  the  finally block
you can use the finally block in one of two ways :---------------

finally {


catch ( Exception e)
             {               // statements
finally {
Whether exception is raised in the try block or not always finally block will be executed.
NOTE: If you want to execute the statement compulsory then you write those statement inside the finally block. Which will be executed  always whether exception will rise or not. Except   system.exit() is executed.
 return statement doesn’t stop finally block execution.

Throw keyword :
Throw keyword :-------it is used to  throw the exception in your own.
Generally JVM will identify the exception class, create the object and throw that object. Sometimes you may get a chance to do this  specially in the case of user defined exceptions.
If you want you can built in exception.
Throws keyword

Throws keyword: ---------“propagate the exception”
Throws keyword is used to specify the method level exception.
Generally when you are implementing a method, code inside that method may rise exceptions. Then you have two alternatives: -----------------------
v  Handle that exception inside the method
v  Propagate to the caller of the method.
If you want to handle the exception in the same method then use try and catch method.
If you want to propagate this exception to the caller pf the method then use throws keyword.
 Checked Exception:
  • Checked exception are exception which are verified at compile time 
  •   Checked exception are also called as compile time exception. 
  •   All the Exception which are subclasses of exception class are called as check Exception or compile time Exception(excluding runtime Exception class and its subclasses)
  Example  of Checked Exception :-----
Class not found.
Attempt to clone an object that does not implement the Cloneable interface.
Access to a class is defined.
Attempt to create an object of an abstract class or interface.
One thread has been intrupted by another thread.
A request field does not exit.
A requested method does not exit.

     Unchecked Exception
  •  Unchecked exception are exception which are verified at run time 
  •   Unchecked  exception are also called as run  time exception. 
  •   All the Exception which are subclasses of Runtime exception are Runtime Exceptions.
   Example  of Unchecked Exception :-----


  • Arithmetic error, such as divide-by-zero.
  • Invalid cast.
  • Array index is out-of-bounds.
  • Assignment to an array element of an incompatible type.
  • Illegal argument used to invoke a method.
  • Illegal monitor operation, such as waiting on an unlocked thread
  • Environment or application is in incorrect state.
  • Requested operation not compatible with current thread state.
  • Some type of index is out- of-bounds.
  • Array created with a negative size.
  • Invalid use of a null reference.
  • Invalid conversion of a string to a numeric format.
  • Attempt to violate security
  • Attempt to index outside the bounds of a string.
  • An unsupported operation was encountered.


Post a Comment