Introduction
When it comes to programming, we are concerned about errors. Whenever you write the code in Java or in any other programming language, the first concern you have is encountering errors. For example, you will get errors when you want to declare a variable but forget to do so and then try to use it.
Must Read, Multithreading in java, Duck Number in Java
How to handle exceptions?
So if we talk about Handling Exceptions , we have two things here. We have errors and exceptions. So basically, when you write a program, there might be many different kinds of exceptions.
For example,
- You are trying to open a file, and the file doesn't exist, so there is an exception.
- You are asking for the user's input, and the user is not giving you the input, so that's an exception.
- You are trying to run a process, but the CPU is unavailable, so that's an exception.
-
You are trying to divide a number by zero, which is also an exception.
So there are different kinds of exceptions if you talk about the exception hierarchy.
We utilize the throw keyword to throw an exception from the code explicitly. Any method or static block can be used. This exception must be a Throwable subclass. It can also be a Throwable in and of itself. We can not throw multiple exceptions with a single throw.
The keyword throws can be used in the method declaration. It specifies the exceptions that this procedure can throw.
Normally, whenever you have an "able" at the end, such as cloneable, closeable, or serializable. All these are "able" interfaces, but this throwable is not an interface; it's a class. And the subclass of throwable is an exception. We must use try-catch to handle these exceptions.
In Java, there are two types of exceptions. They are:
- Checked exceptions
- Unchecked exceptions
Checked exceptions
Checked exceptions are exceptions other than Runtime Exceptions that the compiler examines during compilation to verify if the programmer has handled them. Compilation errors will occur if these exceptions are not handled/declared in the application—for example, IOException, SQLException, ClassNotFoundException, etc.
Unchecked Exceptions
Unchecked Exceptions are another name for Runtime Exceptions. These exceptions are not verified at compilation time; thus, the compiler has no way of knowing whether the programmer has dealt with them or not. The programmer's job is to deal with these exceptions and offer a safe exit. For example, NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException etc.
Example of Basic Exception:
class Exception {
public static void main(String args[]) {
try {
//code that may raise exception
} catch (Exception e) {
// rest of the program
}
}
}
Some of the examples of Java Exception
An exception can occur if it encounters the following issues:
i. Invalid input
ii. Accessing the elements of an array beyond its range
iii. Divide by zero errors
iv. Hard disk crash
v. Heap memory exhausted
vi. Opening a non-existent file
Advantages of Exception Handling
- Error-handling code is separated from typical business logic code.
- Errors are propagating up the call stack.
-
Error kinds are grouped and differentiated.
How to handle exceptions in Java?
As I previously stated, properly managing exceptions is critical, and if we don't handle them, the system will fail. But how do you deal with these exceptions?
To handle Exceptions, Java provides several methods, such as:
- try
- catch
- finally
- Throw
- throws