Java Exception Handling

Java Exception Handling

In java programming language there is a feature called excepton handling to handle run time errors which are caused by exceptions.

What is an exception in java?

An exception is unwanted event, this causes the normal flow of the program to be terminated. Java automatically generates the system error when the exception occurs

Some Basic examples for exceptions

  • opening a unavailable (opening shortcuts present on desktop,where the program is already uninstalled)
  • wrong input (entering alphabets instead of numbers)
  • Loss of network connection
  • Code errors

Hierarchy of Java Exception classes

IMage

Types of exceptions

  1. Checked exceptions
  2. Unchecked Exceptions
  3. Errors

Checked exceptions

All exceptions excluding of run time exceptions are called checked exceprtions. These are the classes which inherits throwable class except run-time exception. These are checked during the compile time. some of the examples are IOException, SQLException

Unchecked expressions

Run time exceptions are know as unchecked expressions because uncked expressions are not checked at the compile time,they are checked at run-time. These are the classess which inherits only RuntimeException class. some of the examples are ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException

Error

ERROR is a non recoverable exceptions examples are VirtualMachineError, AssertionError, OutOfMemoryError etc

Java Exception Keywords

KeywordDescription
trytry keyword is used to create a block, where we can place the exception code. we cant use try block alone, it must be followed by either catch block or finally block
catchthe catch block is used to handle the exception. catch block should be after the try block. we cant use catch block alone
finallyfinally block is used to execute import code, whether there was an exception occurred or not
throwthrow keyword is used to throw an exception to catch block
throwsthrows keyword is used to declared exceptions. this keyword doesn’t throw exceptions
Last updated on by vishal devxo