Core Java Tutorial

 
 

What is Exception in Java

Exception is an event which will occur during the execution of program, which will leads to halt(stop) the normal flow of execution.Exception will occur either at compile time or at Runtime.Exception is subclass of Throwable which is subclass of Object

           java.lang.Object
                java.lang.Throwable
                     java.lang.Exception
Exception Heirarchy

What are different types of Exceptions?
We have two different kinds of Exceptions.They are

  1. Checked Exception
  2. Checked Exceptions are also called compiletime Exceptions because which are occur during compiletime.You can identify checked exceptions in two different ways

          1. If your class inherits from Exception class (or)
          2. If Javac(Java compiler) forces you to handle the code 
     
     Checked Exception examples:
           1.ClassNotFoundException
           2.FileNotFoundException
           3.NoSuchFieldException etc...
    
  3. Unchecked Exception
  4. Unchecked Exceptions are also called Runtime Exceptions because which are occur during Runtime.You can identify Unchecked exceptions in two different ways

          1. If your class inherits from RuntimeException class (or)
          2. If Javac(Java compiler) does't force you to handle the code 
     
     Unchecked Exception Examples:
           1.ArithmeticExceptions
           2.NullPointerException
           3.ArrayIndexOutOfBoundsException etc...
    

Exception Handling

Exception is handled by using following keywords

  1. try
  2. catch
  3. finally
  4. throws
  5. throw

1.Java try block

In try block, we will put the code where you might think that you will get an exception. once if you get the exception then the controller will goes to catch block.

     syntax:
             try{
               .....
                 }

2.Java catch block

After getting the exception from try block, those exceptions will be handled in catch block.This block should be always after the try block and you can have more than one java catch block in java.

     syntax:
              catch(Exception e){
               .....
                 }

3.Java finally block

This is optional, you can use if you want or you don't need to use if you don't want.finally block will be placed either after try block if you don't use catch block or after catch block if you catch block after try block.you may have 0 or 1 finally block.It is also used to close the connections.

sample program

     publi class ExceptionDemo{
       public static void main(String[] args){
          int a=0;
          int b=4;
          try{
              int c=b/a; // this is not valid and here you will get the exception
                         // that's why i am putting that in try block
              }
          cath(ArithameticException e){ 
            // Here Exception is object and e is object reference variable
           e.printstackTrace();
              }
           finally{
               System.out.println("Here you will get the Arithmetic Exception");
              }
            }
          }

4.throws keyword

If method did't handle the exception then you must declare that exception using thows keyword and throws keyword is declared after the method declaration.You can declare more than one exception using the coma(,) operator.In this case if you call this method then that particular exception should be caught or you should declare that particular exception class.

     Ex:
               public void add() throws ArithmeticException{
                   int a=0;
                   int b=4;
                   int c=b/a;
               }

5.throw keyword

This throw keyword id used to throw an exception explicitly(i.e you can give the name in whatever fashion you want to display to the end user).This keyword is used inside the method and by using this you can throw either checked exception or unchecked exception.

     Ex:
               public class ThrowDemo{
                 double savings;
                public void withdrawMoney(double WithdrawAmount){
                 if(WithdrawAmount>savings){
                     throw new ArithmeticException("please enter valid WithdrawAmount");
                    } 

Key points in exception handling

1.Only one exception will occur at a time and only one catch block will be executed at a time.
2.If you have more than one catch block then your subclass exception should be before super class exception.
3.after the try block there must be at least either catch or finally block.
4.nested try block is also possible like nested if.
5.catch block should be always after the try block, you can have more than one catch block.
6.finally block is optional. you can have either 0 finally blocks or only 1 finally block.
7.If a method doesn't handle the exception then it should use throws keyword.
8.If subclass wants to throw any exceptions its superclass should throw the same exception.
9.there should be no code present in among try, catch and finally block's.