Showing posts with label exception handling. Show all posts
Showing posts with label exception handling. Show all posts

Sunday, 8 February 2015

Try And Finally Without Catch Block

1. We Can Use Finally after try block ,it will run gives message in finally block.But can not handle exception.
Syntax:

try{


}Finally{


}

public class ExceptionSample {
public static void main(String[] args) {
try {
int i=10/0;
}finally{
System.out.println("Finally Block");
}
System.out.println("You Are Come Back In Main");
}

}

Output:
Exception in thread "main" Finally Block
java.lang.ArithmeticException: / by zero
at exception_handling.ExceptionSample.main(ExceptionSample.java:6)

Nested Try Blocks In Java

We can use nested try block in java.

Syntax:1


       try {

            // some statement
try {

               // some statement
} catch (Exception e) {

}

} catch (Exception e) {

}

Syntax:2


try{

display();

}catch(){

}

void display(){

try{

}catch(){

}


}





1. 

public class NestedTry {


public static void main(String[] args) {


try {
 
System.out.println("Outer Try");
int []arr=new int[5];
arr[10]=100;
try {
System.out.println("Inner Try");
int i=100/0;
} catch (Exception e) {
System.out.println(e);
}

} catch (Exception e) {
System.out.println(e);
}


}



}

Output:

Outer Try
java.lang.ArrayIndexOutOfBoundsException: 10

Explanation: See This  Interesting Facts In Exception Handling In java

2.


package exception_handling;

public class NestedTry {

public static void main(String[] args) {

try {
 
System.out.println("Outer Try");
int []arr=new int[5];
try {
System.out.println("Inner Try");
int i=100/0;
} catch (Exception e) {
System.out.println(e);
}
} catch (Exception e) {
System.out.println(e);
}
}

}

Output:

Outer Try
Inner Try
java.lang.ArithmeticException: / by zero


3.

package exception_handling;

public class NestedTry {

void divideNumber(){
try {
System.out.println("In divide number");
int j=10/0;
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {

try {
 
System.out.println("Outer Try");
  int []arr=new int[5];
           NestedTry nestedtry=new NestedTry();
           nestedtry.divideNumber();
} catch (Exception e) {
System.out.println(e);
}
}

}

Output:

Outer Try
In divide number
java.lang.ArithmeticException: / by zero

Multiple Catch Block In Exception Handling In java

1.  We can use multiple catch block for exception handling.

Syntax:

try {
}
catch () {
}
catch () {
}
catch () {
}

Example :


public class MultipleCatch {
public static void main(String[] args) {
int []arr=new int[5];

try {
             
arr[10]=100;
}
catch (ArithmeticException e) {
System.out.println(e);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}

}
}



Output:

java.lang.ArrayIndexOutOfBoundsException: 10

2:     Remember that when we use multiple catch block , use catch block down to top hierarchy. 
   Because in java unreachable code gives error. 

  
  public static void main(String[] args) {
           int i=10;
           int j=0;
           try {
               System.out.println("i/j="+i/j);
        } catch (Exception e) {
            System.out.println(e);
        }
         catch (IndexOutOfBoundsException e) {
                System.out.println(e);
        } 
          
       }

Output:
Unreachable catch block for IndexOutOfBoundsException. It is already handled by the catch block for Exception 

3.  The right way of uses multiple catch block is use first specific catch handle then use  standard catch handle.(i.e. down  to top ).

Example:

public class simple_example {

        public static void main(String[] args) {
           int i=10;
           int j=0;
           try {
               System.out.println("i/j="+i/j);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("index out of bound"+e);
        }
         catch (ArithmeticException e) {
                System.out.println("Arithmetic="+e);
        } 
         catch (Exception e) {
             System.out.println(e);
        } 
           
       }
}

Output:

Arithmetic=java.lang.ArithmeticException: / by zero










Saturday, 7 February 2015

How to Handle Exception In Java


1.
public class simple_example {

        public static void main(String[] args) {
      int i=10;
      int j=0;
      try {
      System.out.println("i/j="+i/j);
} catch (Exception e) {
//System.out.println(e);
System.out.println("number divide by zero");
}
   
       }
}

Output:
 number divide by zero


Exception Handling In Java



  •    An exception is a event ,which interrupt the normal flow of the program.
  •     Throwable class is the super class of all errors and exceptions in java.
  •    We can explicitly throw an exception using 'throw' clause.