![]() |
VOOZH | about |
Exception Handling in Java is a mechanism used to handle both compile-time (checked) and runtime (unchecked) exceptions, allowing a program to continue execution smoothly even in the presence of errors.
Error: Division by 0!
The finally block executes after the try and catch blocks in most situations, whether an exception arised or not. It is typically used for closing resources such as database connections, open files, or network connections.
Finally may not execute in cases like:
Exception caught: java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3 This block always executes. Program continues...
1. throw: Used to explicitly throw a single exception. We use throw when something goes wrong (or “shouldn’t happen”) and we want to stop normal flow and hand control to exception handling.
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Age must be 18 or above
at Demo.checkAge(Demo.java:5)
at Demo.main(Demo.java:11)
2. throws: Declares exceptions that a method might throw, informing the caller to handle them. It is mainly used with checked exceptions (explained below). If a method calls another method that throws a checked exception, and it doesn’t catch it, it must declare that exception in its throws clause
File not found or error reading file: test.txt (No such file or directory) Program continues after file operation.
Internal Working of try-catch Block:
try block.try code is skipped and JVM searches for a matching catch block.catch block executes.finally block (if present).catch is found, the exception is handled by JVM’s default handler.finally block always executes, whether an exception occurs or not.Note: When an exception occurs and is not handled, the program terminates abruptly and the code after it, will never execute.
In Java, all exceptions and errors are subclasses of the Throwable class. It has two main branches
The below figure demonstrates the exception hierarchy in Java:
Java defines several types of exceptions that relate to its various class libraries. Java also allows users to define their it's exceptions.
Built-in Exception are pre-defined exception classes provided by Java to handle common errors during program execution. There are two type of built-in exception in java.
To know more about Checked and Unchecked Exception -> Checked and Unchecked Exception
Sometimes, the built-in exceptions in Java are not able to describe a certain situation. In such cases, users can also create exceptions, which are called "user-defined Exceptions".
In Java, you can place one try-catch block inside another to handle exceptions at multiple levels.
Outer try block Inner catch: java.lang.ArithmeticException: / by zero Outer catch: java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null
We can handle multiple type of exceptions in Java by using multiple catch blocks, each catching a different type of exception.
When an Exception occurs, the JVM creates an exception object containing the error name, description, and program state. Throwing an exception means creating an exception object and transferring control to the nearest appropriate exception handler using the throw keyword. There might be a list of the methods that had been called to get to the method where an exception occurred. This ordered list of methods is called call stack. Now the following procedure will happen:
Exception in thread "abc" Name of Exception : Description
// Call Stack
Look at the below diagram to understand the flow of the call stack:
Illustration:
Output:
| Feature | Exception | Error |
|---|---|---|
| Definition | An event that occurs during program execution, disrupting normal flow, which can be handled using try-catch. | A serious problem that occurs in the JVM, generally cannot be handled by the application. |
| Package | java.lang.Exception | java.lang.Error |
| Recoverable | Yes, can be caught and handled. | No, usually not recoverable. |
| Examples | IOException, SQLException, ArithmeticException | OutOfMemoryError, StackOverflowError |