DEV Community

Deepikandas
Deepikandas

Posted on • Edited on

#27 Exception Handling in JAVA -part1

What is an Exception?
An unwanted or unexpected event that interrupts or disturbs the normal flow of the program is called an Exception.
Example:

Exception Hierarchy in Java:
Java exceptions can be of several types and all exception types are organized in a fundamental hierarchy as shown below.

Exception Vs Errors
Errors
Errors are serious problems that occur due to system-level failures and are generally beyond the control of the application. They are not meant to be caught or handled by the program.

  • Caused by JVM or system failures
  • Programs are usually unable to recover

Types of Exception:


Built-in EXception:
1. Checked Exception:

  • Checked exceptions are those exceptions that the compiler checks at compile time to ensure they are either handled using try-catch or declared using throws, so that the program can run smoothly without unexpected runtime failures.
  • All exceptions in Java that are not subclasses of RuntimeException (and its subclasses) and not subclasses of Error (and its subclasses) are classified as checked exceptions. 2. Unchecked Exception: The exception which is not checked by the compiler is called Unchecked exception. This exception occurs during the run time of the program execution. All Runtime exception and its child classes, Error and its child classes fall under Unchecked exceptions. Exception Handling:
  • Exception handling by JVM:

    2.Exception handling by Programmer using code:
    In Java, a programmer can handle exceptions via five keywords: try, catch, finally, throw and throws.

  • 1.try..catch
    try:
    A try block is used to enclose risky code that might throw an exception.
    Java runs the code inside try
    If an error occurs, control moves to catch
    If no error occurs, catch is skipped
    catch:
    A catch block is used to handle and respond to exceptions that occur inside a try block.
    If an exception occurs in try, control moves to catch
    catch prevents program from crashing
    It defines what to do when an error happens


Remaining Statements in try Block Are Not Executed
When an exception happens inside a try block, the normal flow is interrupted. The remaining statements in the try block are skipped. Control immediately moves to the matching catch block to handle the error. After handling, the program continues with the code after the try-catch.

2.try..catch...finally
finally:

  • A block that always runs after try-catch execution, even if an exception occurs or not.
  • finally is a block that always executes after try (and catch, if present)
  • finally ensures important cleanup tasks (like closing files, releasing resources, and closing connections) always run.  Can try exist without catch? A try block must be followed by at least one of these: catch finally


Nested try:
How it works (simple)
If error happens in inner try, inner catch handles it
If inner catch cannot handle it, it goes to outer catch
Each try has its own catch block


Common Exception
Exception is the common parent class of most exceptions in Java.
It can be used to catch both checked and unchecked exceptions.
It does not include Error types (like OutOfMemoryError).

Top comments (0)