Exception Handling in Java
Skip to main content

Exception Handling in Java

 


exception handling in java, exception handling in java interview questions, exception handling in java javatpoint, exception handling in java with examples, exception handling in javascript, exception handling in java example, throws and throw in java, throws and throw java



Exception Handling in Java


Introduction:-  Exception Handling in Java in this tutorial we discuss about mechanism of  exception handling in Java. Exception Handling is a run time error management into the object oriented world.

Exception Handling in Java:- 

Java exception is when a object create the exceptional (error) condition in the source code and object is representing that error and throw that error in a method. That method caught that error and processed. That process is called Exception Handling. Java exception handling managed via five keywords try, catch, throw, throws and finally

Exception handling Syntax :- 

 try{
               // block of code for moniter error
            }
catch( Exception_type1 exception_object){
              // exception handling for Exception_type1
          }
catch( Exception_type2 exception_object){
        // exception handling for Exception_type2
          }
     finally{
         // block of code execute after try block end.
         }


try and catch block:- 

try block contains the source code. If any error occur in a source code which is written in try block then try block check that error at run time and throw the error. try block is close by Carly braces. try block syntax is:-
       try{
               // block of code for monitor error
            }

Catch block is used to catch that exception. There write a type of exception and defined objects of Exception Types as a parameter of catch block . for a example:-

catch( Exception_type1 exception_object){
              // exception handling for Exception_type1
          }

When we not use  try catch block then error show and program terminate at run time.
 
class TryCatchExample{
    int a=30; int b=0; int div;
       public static void main(String [] args){
           div=a/b;
            System.out.println(div);
       }            
    }
     

If we use a try/catch block in a source code statement then after catch block is execute that throw the error but its any subsequent statement is executed. :-

class TryCatchExample{
    int a=30; int b=0; int div;
       public static void main(String [] args){
          try{
           div=a/b;
            System.out.println(div);
            }
          catch(AirthmeticException e){
               System.out.println(e);
            }
         System.out.println("Out of the try catch block");
       }            
    }

Output:-  Java.lang.AirthmeticException: / by zero
               Out of the try catch block

Most Read:- Data Types in Java

User can also defined the exception as per his requirement  in his code for example


class TryCatchExample{
    int a=30; int b=0; int div;
       public static void main(String [] args){
          try{
           div=a/b;
            System.out.println(div);
            }
          catch(AirthmeticException e){
               System.out.println("You divide by Zero");
            }
       }            
    }

Output:-  You divide by Zero


throw :- 

It is possible that user can throw a exception  in his program, using the throw statement.  
 general form of throw statement is :
                   
      Syntax:   throw Throwableinstance;
               
                Throwable instance  must be an object of type Throwable class but we cannot use primitive and non primitive data type for the exception. There are two ways to can obtain a Throwable object: First we using a parameter in a catch statement and second is creating one with the new operator.
            The flow of execution stops immediately after the throw statement is called. any subsequent statement is not executed. 

  

class ThrowExample{
    
       public static void main(String [] args){
          try{
           
               throw new AirthmeticException("Divided by Zero");
            }
          catch(AirthmeticException e){
               System.out.println(e.getMessage());
            }
       }            
    }

Output:-  Divided by Zero

throws :- Including the throws keyword with the declaration of the method. A throws clause use with the method and throw the type of the exceptions. 
 
 Syntax:  access modifier return_type method_name() throws exception_type{
                     // Source code
                 }

Example:- 

             class ThrowsExample{
                           
              public void throwsDemo() throws IllegalAccessException{
                   System.out.println("Inside throwsDemo ");
                    throw new IllegalAccessException("throwsDemo method Illegal Access Exception");
                }
    
             public static void main(String [] args){
              try{
                    throwsDemo();
                
                }
                catch(IllegalAccessException e){
                      System.out.println("Inside catch Block"e.getMessage());
            }
       }            
    }

Output:- Inside throwsDemo
              Inside catch Block throwsDemo method Illegal Access Exception




Finally :-  finally block is created that will be executed after the try/catch block has completed. The finally block will  execute whether  any exception error occur or not in the try catch block. finally block execute without of catch block with try block. this is generally use the finally block to execute clean up code. try block require at least on  of catch block or finally block.

1. When not any exception in a program;- 

  class FinallyExample{
    int a=30; int b=5; int div;
       public static void main(String [] args){
          try{
           div=a/b;
            System.out.println(div);
            }
          catch(AirthmeticException e){
               System.out.println(e);
            }
         finally{
             System.out.println("Inside the finally block");
          }
         System.out.println("Out from all  block");
       }            
    }

Output:- 6
            Inside the finally block
            Out from all  block

2. When  any exception occur in a program catch block is not caught error:-

class FinallyExample{
    int a=30; int b=0; int div;
       public static void main(String [] args){
          try{
           div=a/b;
            System.out.println(div);
            }
          catch(IlligalAccessException e){
               System.out.println(e);
            }
         finally{
             System.out.println("Inside the finally block");
          }
         System.out.println("Out from all  block");
       }            
    }

Output:- Inside the finally block
           Exception in thread "main" java.lang.ArithmeticException: / by Zero at FinallyExample.main(FinallyExample.java: 10)

3. When  any exception occur in a program and catch block caught error:-

class FinallyExample{
    int a=30; int b=0; int div;
       public static void main(String [] args){
          try{
           div=a/b;
            System.out.println(div);
            }
          catch(ArithmeticException e){
               System.out.println(e.getMessage());
            }
         finally{
             System.out.println("Inside the finally block");
          }
         System.out.println("Out from all  block");
       }            
    }

Output:- / by Zero
           Inside the finally block
           Out from all  block

Most Read:- Features of Java

Post a Comment

0 Comments