Core Java Tutorial

Core Java Tutorial

What is Exception in Java

Exception in Java:

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

  • RunTimeException or Unchecked Exception
  • CompileTimeException or checked Exception
  • 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

  • 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.

  • 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.

  • IOError
  • LinkageError
  • VirtualMachineError