Fallowing icon

Saturday, April 5, 2014

Java Exception Handling


     Exception handling is very impotent in software development. When we are getting user input or work with database, exceptions can be thrown. In this case we want to handle those exceptions correctly. If we forget handled exceptions in our program, it's can be stuck due to the exception. We can handle the exception using throw, throws, try-catch or try-catch-final keywords.

                  
1) throws :- 
                       Before throw an exception we  tell to the compiler, this part can be throw exception or this is risk area, but we cannot handle the exception using throws keyword. It's mean when the exception is thrown program can be stuck. This keyword helps to success compilation without throw exception.  

Eg:- 

                  import java.io.BufferedReader;
                  import java.io.InputStreamReader;
                  import java.io.IOException;

                  public class excepiton{
                           public void test()throws IOException{          // this method can be throw Input output exception
                                     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                                     System.out.print("Enter the number :- ");
                                     int s=Integer.parseInt(br.readLine());
                                     System.out.println("You Entered :-"+s);    
                                     }
                           public static void main(String args[])throws IOException{ // if you call method with risk, must use throws keyword for each method, witch call to risk method
                                     excepiton a=new excepiton();
                                     a.test();
                           }
                   }


2) try-catch
                      If program throw exception, catch it and handle that exception.
                   
                      The try statement has this general forms:

                       1)
                                  try {
                                             statement that can throw exceptions
                                  }
                                  catch (exception type identifier){
                                             statement executed when exception is thrown
                                  }

                        2)
                                  try {            
                                             statement that can throw exceptions
                                  }
                                  catch (exception type identifier){
                                             statement executed when exception is thrown
                                  }
                                  finally {                                                     //optional
                                              statement that are executed whether or not exceptions occur
                                  }
                  
                         3)      in java 7 or later version, can use more than one catch close for each try block

                                  try {
                                             statement that can throw exceptions
                                  }
                                  catch (exception type identifier){
                                             statement executed when exception is thrown
                                  }
                                  catch (exception type identifier){
                                             statement executed when exception is thrown
                                  }
                                  catch (exception type identifier){
                                             statement executed when exception is thrown
                                  }
                                  ...............
                                  ...............


                                  finally {                                        //optional
                                              statement that are executed whether or not exceptions occur
                                  }


Eg:-

                       public class ExceptionExample {
                                 int x=4,y=0;
                                 private int divideByZero (){
                                       int cal=0;
                                       try{            
                                               cal=x/y;                // exception occur "Divide by zero" (y=0)
                                       }catch(ArithmeticException e){System.out.println(e.getMessage());}
                                        finally{
                                               if (y==0)
                                                      System.out.println("y can't equal to zero ");                                               
                                        }
                                         return cal;
                                        }
     
                                  public static void main(String args[]){
                                          ExceptionExample a=new ExceptionExample();
                                          System.out.println(a.devideByZero());       
                                  }
                           }

output in net beans :-
                           / by zero
                           y cannot equal to zero 
                           0

              This code can compile without problem. But when you run it, code throw Arithmetic Exception called divide by zero. When exception it occur  at cal=x/y line, program jump catch close and execute it. finally program execute finally close.
            
        


3) throw :- 
                        throw keyword is used for generate new exception in java.
                   public class ThrowExample {

                  public Object exThrow() throws EmptyStackException {
                  if (Empty()) throw new EmptyStackException();
                  ...
                  }
                   }
  • Exceptions are objects, so you can't simply throw an  EmptyStackException. you must use "new" keyword to create an exception object.
  • You must use in throw keyword between try-catch or under throws keyword.

No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment