Tuesday, 8 November 2016

Java Exception Handlling

Exception Handling -
It is the mechanism to handle the runtime errors so that the normal flow of application is maintained.

2 types of exceptions -
1.Checked
2.Unchecked

 Checked Exceptions- checked at the compile time and they must be catched (using catch block).
 All sub-exceptions of Exception class except Runtime Exception are checked exception.
  eg. IOException
        SQLException
        ClassNotFoundException

Unchecked Exception- Checked at the runtime.
eg. ArithmeticException
     NullPointerException
    ArrayOutOfBoundException

How to handle?
 1.try-catch
 2.try-finally
 3.try-multiple catch  -
    Only one catch is executed.
    Exceptions must in order of specific to generic in case of multiple catch use.
    Only 1 finally loop can be used.
  4.throws keyword(both checked and unchecked)

Exception Propagation-
Unchecked -> Automatically propagates from current method to previous method and so on until it is catched.
Checked  -> Automatically does not propagate.
                      Must be handled by using throws method signature. 
link->http://www.javatpoint.com/exception-propagation

Throws only declare exception but for smooth flow it must be handled.
              1.Declare Throws -> and Exception Occurs -> It will throw exception at runtime.
              2.Declare Throws -> and Handle Exception with try-catch               




No comments:

Post a Comment