Exception Handling

Exception Handling


Information regarding Exception:-
  1.  Dictionary meaning of the exception is abnormal termination.
  2. An expected event that disturbs or terminates normal flow of execution called exception.
  3. If the application contains exception then the program terminated abnormally the rest of the 
    application is not executed.
  4. To overcome above limitation in order to execute the rest of the application must handle the 
    exception.
In java we are having two approaches to handle the exceptions.
1)  By using try-catch block.
2)  By using throws keyword.

Exception Handling:-
  1. The main objective of exception handling is to get normal termination of the application in order to execute rest of the application code.
  2. Exception handling means just we are providing alternate code to continue the execution of remaining code and to get normal termination of the application.
  3. Every Exception is a predefined class present in different packages.
  • java.lang.ArithmeticException
  • java.io.IOException
  • java.sql.SQLException
  • javax.servlet.ServletException

The exception are occurred due to two reasons
     a. Developer mistakes
     b. End-user mistakes.
           i.  While providing inputs to the application.
           ii. Whenever user is entered invalid data then Exception is occur.
           iii.A file that needs to be opened can’t found then Exception is occurred.
           iv. Exception is occurred when the network has disconnected at the middle of the
                communication.

Types of Exceptions:-
As per the sun micro systems standards The Exceptions are divided into three types
1) Checked Exception
2) Unchecked Exception
3) Error

1.Checked Exception:-
  1. The Exceptions which are checked by the compiler at the time of compilation is called Checked Exceptions.  IOException,SQLException,InterruptedException……..etc
  2. If the application contains checked exception the code is not compiled so must handle the checked Exception in two ways               
  •               By using try-catch block.
  •               By using throws keyword.
     3.If the application contains checked Exception the compiler is able to check it and it will give
        intimation to developer regarding Exception in the form of compilation error.

There are two types of predefined methods
 1.Exceptional methods
  •            public static native void sleep(long)throws java.lang.InterruptedException
  •           publicbooleancreateNewFile() throws java.io.IOException
  •            public abstract java.sql.StatementcreateStatement() throws java.sql.SQLException
 2.Normal methods
  •      public long length();
  •      public java.lang.StringtoString();

In our application whenever we are using exceptional methods the code is not compiled because these
methods throws checked exception hence must handle the exception by using try-catch or throws
keywords.


2.Unchecked Exception:-

  1. The exceptions which are not checked by the compiler at the time of compilation are called  unchecked Exception.ArithmeticException,ArrayIndexOutOfBoundsException,NumberFormatException….etc
  2.  If the application contains un-checked Exception code is compiled but at runtime JVM(Default Exception handler) display exception message then program terminated abnormally.
  3. To overcome runtime problem must handle the exception in two ways.
  •  By using try-catch blocks.
  •  By using throws keyword.
Example :- Different types of unchecked exceptions
1:  class Test  
2:  { public static void main(String[] args)  
3:  { //java.lang.ArithmeticException: / by zero  
4:  System.out.println(10/0);  
5:  //java.lang.ArrayIndexOutOfBoundsException  
6:  int[] a={10,20,30};  
7:  System.out.println(a[5]);  
8:  //java.lang.StringIndexOutOfBoundsException  
9:  System.out.println("pritam".charAt(10));  
10:  }  
11:  }  

Note-1:-
    If the application contains checked exception compiler generate information about
exception so code is not compiled hence must handle that exception by using try-catch block
or throws keyword it means for the checked exceptions try-catch blocks or throws keyword
mandatory but if the application contains un-checked exception try-catch blocks or throws
keyword is optional it means code is compiled but at runtime program is terminated
abnormally.


Note 2:-
In java whether it is a checked Exception or unchecked Exception must handle the
Exception by using try-catch blocks or throws keyword to get normal termination of
application.

Note-3:-
In java whether it is checked Exception or unchecked exceptions are occurred at runtime
but not compile time.

3.Error:-
   Errors are caused due to lack of system resources like,
   Heap memory full.
   Stack memory problem.
  AWT component problems…..etc
Ex: - StackOverFlowError, OutOfMemoryError, AssertionError…………etc


Comments

Popular posts from this blog

Multithreading Interview Question for Fresher and Experienced

Collections Framework Interview Questions for Fresher and Experienced