Exception Handling In Java

What is Exception in java?

Exception Handling  is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained.

What is Exception Handling?

Exception Handling is a mechanism to handle runtime errors .

Hierarchy of Exception classes

Types of Exception:

There are 2 types of exception

1)Checked Exception.

2)Unchecked Exception.

*Error we can consider as an exception as an unchecked exception.

Checked Exception: he classes that directly inherit the Throwable class except Runtime Exception and Error are known as checked exceptions. For example, IO Exception, SQL Exception, etc. Checked exceptions are checked at compile-time.

Unchecked Exception: The classes that inherit the Runtime Exception are known as unchecked exceptions. For example, Arithmetic Exception, NullPointerException, ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

Error: Error is irrecoverable. Some examples of errors are OutOfMemoryError, VirtualMachineError, Assertion Error etc.

But we can handle with try catch and finally block

try {

//Code may occur exception

} catch (Exception e or type of exception ) {

//Rest of the code Here

} finally {

// Close all open connections Here