What is exception?

      No Comments on What is exception?




Any unwanted or unexpected event which occurs during the execution of a program and it disrupts the flow of program , then it is called exception.

Exception must be handled for the smooth execution of a program. It is highly recommended.
e.g.

class Test{
  public static void main(String []args){
    String str = null;
    int length = str.length();
    System.out.println("Length of Str="+length);
  }
}

Here, the flow of the program gets disrupted. Exception occurs as method length() is being called on null String. Hence, for smooth execution of the program, it is recommended to handle exception for smooth execution and proper message.


    try{
        // Code which can produce exception
    }catch(Exception ex){
       // Recovery code
       // processing logic in case of exception
    }