In this article, we will learn about how to handle exception in java, its types and the difference between checked and unchecked exceptions. An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e. at run time, which interrupts the normal flow of program instructions. Exceptions can be caught and handled by the program. When a method throws an exception, it creates an object. This object is called an exception object. It contains information about the exception, such as the name and description of the exception and the state of the program when the exception occurred. Lets see how to handle exception in java.
How To Handle Exception In Java
The process of dealing with exceptions is known as handling exception in java. Handling exception in Java is a powerful mechanism for handling runtime errors so that the normal flow of the application is maintained. Lets see how to handle exception in java by using try and catch block with example.
How To Handle Exception In Java By Using Try And Catch Block:
The “try” block is used to specify the block where we should place the exception code. This means we cannot use a try block alone.
A “catch” block is used to handle exceptions. It must be preceded by a try block which means we cannot use a catch block alone.
Try catch blocks is the simplest method to handle exception in java. Write the code you want to run inside a try block and any Java exceptions thrown by the code are caught by one or more catch blocks. This method will catch any Java exception that is thrown.
The “finally” block is used to execute the necessary code of the program. It is executed whether an exception is handled or not.
How To Handle Exception In Java example:
java.lang.ArithmeticException: / by zero
rest of the code
Why we handle exception in java
The core advantage of to handle exception in java is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application that is why we need to handle exceptions. Let’s consider a scenario:
Suppose there are 10 statements in a Java program and an exception occurs at statement 5 then rest of the code will not be executed that is statements 6 to 10 will not be executed. However, when we perform exception handling, the rest of the statements will be executed. That is why we handle exception in Java.
Types of Exception in Java
1. Checked Exception in Java
Checked Exception which is also known as compile-time exceptions.
Checked Exceptions are those exceptions that the compiler checks at compile time. If there is a checked exception inside a method then the method should handle the exception or it may mention the exception by using throws keyword.
Examples of checked Exceptions
2. Unchecked Exception in Java
Java Unchecked Exception which is also known as Runtime Exceptions.
Unchecked exceptions are those exceptions that occur at runtime. It completely depends on the programmer whether he or she wants to handle the exception or not. The exceptions under the Error class and the Runtime Exception class are unchecked exceptions.
Examples of Unchecked Exceptions
Q 1. How To Handle Exception In Java using try and catch block?
- Write a code in try block which might throw an exception.
- one or more catch blocks that handle the exception.
- A finally block which gets executed after the try block was successfully executed or a thrown exception was handled.
Q. 2 How To Handle Exception In Java without using try and catch block?
We can also handle exception in java by using throws keyword. It specifies the exceptions that a method can throw to the caller and does not handle itself.