This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Sunday, May 9, 2021

what is exception handling in java

 what is exception handling in java


When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.


After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The list of methods 




What is exception handling in Java with example?

Exception handling ensures that the flow of the program doesn't break when an exception occurs. For example, if a program has bunch of statements and an exception occurs mid way after executing certain statements then the statements after the exception will not execute and the program will terminate abruptly.




example exception handling in java:



public class JavaExceptionExample{  

  public static void main(String args[]){  

   try{  

      //code that may raise exception  

      int data=100/0;  

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

   //rest code of the program   

   System.out.println("rest of the code...");  

  }  

}  

No comments:

Post a Comment