Java Exception handling and its types


What is exception?

An exception is an event, which occurs during the execution of a program, which disrupts the normal flow of the program's instructions.

 

Exception handling?

Exception is the place where a problem has occurred, Handling is the place for the solution to the specific exception.

Use try block to write code that may disrupts the normal program flow,

Use catch block to handle it

Syntax

try {
    // code where an exception may occur
    }
    catch (Exception e)
    {
	e.printStackTrace();
    }


Types of exception

                There are three types of exception

1.       RunTimeException or Unchecked Exception

2.       CompileTimeException or checked Exception

3.       Error

 

RuntimeException or Unchecked Exception

                                These type of exception can be easily validated during code development. A developer can avoid this exception completely in their application by writing good validation. Exception that are subclass of RuntimeException are called as Unchecked Exception

Example

               ArithmeticException

               NumberFormatException

               NullpointerException


CompileTimeException or checked Exception

              Exception that cannot be validated during development are called checked Exception, the compiler will force you to use try catch block or to throws the exception.

Example

               FileNotFoundException

               SQLException

               InterruptedException


Error

                An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

Example

               IOError

               LinkageError

               VirtualMachineError





Comments


©candidjava.com