A Java exception is an object that describes an exceptional condition which has occurred in a piece of code. Many things can lead to exceptions , including hardware failures, resource exhaustion and bugs in the program. When an exceptional event occurs in the Java program , an exception is thrown.  The code that is responsible for doing something about the exception is called “exception handler”.

            The Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. We have tell the JVM what code to execute when certain exceptional condition occurs in the program. To do this we use try and catch keywords. Within a try block we will write a code in which exception may occur. This block of code is called guarded region. One or more catch clauses  match a specific exception to a block of code that handles it.


            Here is the basic form of exception handling block.



                                //block of code

                        }catch ( ExceptionType1 ex1){

                                    // exception handler for ExcpetionType1.

                        } catch(ExceptionType2 ex2){

                                    //Exception handler for ExcpetionType2.




Java Exception Type Hierarchy:.
















·         Throwable class is used represent all exceptional conditions. This is superclass of all Exception types.

·         Two immediate  subclass of Throwable class is Error and Exception.

·         Exception is used for exceptional conditions that user programs should catch.

·         Exception class is used to create our own custom exception. Just we have to create a subclass of Exception and we create our exceptional conditions in this class that user program to catch.

·         Example for Exception Class

            public  class Propagate {


              public static void main(String[] args) {

                     String str="pramila";

                     Propagate p=new Propagate();


                                  String revStr=p.reverse(str);


                           }catch(StringIndexOutOfBoundsException stEx){


                           }catch(IndexOutOfBoundsException ie){



                           System.out.println("Exception occured in finally block");


                                         System.out.println(" In finally block try block                                          Value"+5/0);

                                  }catch(ArithmeticException ie){

                                         System.out.println("Exception                                                            occured"+ie.getMessage());






       public String reverse(String str){

              String reveString = "";

              for(int i=str.length();i>0;i--){

                     reveString += str.charAt(i);


              return reveString;




·         Throw statement is used to explicitly throw an exception. First we have to get a handle on an instance of Throwable, via a parameter in to a catch clause or by creating one using  new operator. General form of throw statement is throw ThrowableInstance;

·         Throws keyword is used to identify the list of possible exceptions that a method might throw. General form of throws statement is type method-name(arg-list) throws exception-list {}

·         Sometimes it is necessary to be assured that given piece of code will run no matter which exceptions are caused and caught. The finally keyword can be used to identify such a block of code.

·         It is illegal to use a try clause without either a catch or finally  clause.

·         There are two types of exceptions

   Checked Exception: - Checked exception must be explicitly caught or propagated. It                                                      extends the class java.lang.Exception


Unchecked Exception: Is not explicitly caught or propagated. It extends the the class                                                                    java.lang.RuntimeException




·         Error defines the conditions that should not be expected to be caught under normal circumstances. Error are typically created in response to catastrophic failures. For Example StackoverflowError. The most common way this to occur is to create a recursive method. Look at the following code

                  void go(){



            Here if we make a mistake of invoking the go() method, our program will fall into a black hole; go invoking go() invoking go, until, no matter how much memory we have, we will get a StackOverflowError. Here only JVM knows when this event occurs, and the JVM will be the source of this error.

Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)