Friday, February 13, 2015

Exception

Exception highlights/handles the errors. Please find the example how to catch the exception and display so that we can take an appropriate action on the statement that is triggering this.


package Example1;

public class ExceptionExample {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  dividebyzero(2);
 }
 public static void dividebyzero(int a){
  try{
   a=a/0;
  }
  catch(Exception e){
   System.out.println(e.getMessage());
   System.out.println(e.toString());
   e.printStackTrace();
  }
 }
}

output:
/ by zero
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
at Example1.ExceptionExample.dividebyzero(ExceptionExample.java:11)
at Example1.ExceptionExample.main(ExceptionExample.java:7)

>>>Exceptions
------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment