In this lesson, you will learn
Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
The following are the common exceptions thrown by JVM
Note: You need to handle and manage the above exceptions; otherwise, the program may be terminated suddenly without showing the desired output.
Java provides the following keywords for handling exceptions: try, catch, finally, throw and throws.

try:try block encloses the code that might throw an exception.catch:catch block handles the exception that is thrown within the corresponding try block.catch blocks to handle different types of exceptions.finally:finally block contains code that is always executed, regardless of whether an exception is thrown or caught.throw:throw keyword is used to explicitly throw an exception.throws:throws keyword is used in method declarations to indicate that a method might throw a particular exception.
try-catch block is used to catch and handle exceptions.catch block.The following is the syntax of exception handling through the try-catch block.
try {
// Code that might throw an exception
} catch (ExceptionType1 e1) {
// Handle ExceptionType1
} catch (ExceptionType2 e2) {
// Handle ExceptionType2
} finally {
// Code that always executes (cleanup, etc.)
}
The following figure shows the working process of try-catch-finally block to handle the exception in Java.

If an exception is raised within the try block, the appropriate exception handler that is associated with the try block processes the exception.
In Java, the catch block is used to handle exceptions and is known as an exception handler.
A try block must have at least one catch block that follows the try block, immediately. No other statements are allowed between try-and-catch blocks.
The catch block specifies the exception type that you need to catch or handle.
catch(ArithmeticException e) { //Statements }
//This catch block will handle the ArithmeticException
// E.g. '/' division by zero.
The try block can generate more than one type of exception. If any statement generates an exception, the remaining statements in the try block are skipped and control will be transferred to the appropriate catch block.
The catch block can also have more than one statement that handles the exception.
Consider the following example where you will accept two integers from the user, and perform some arithmetic operation.
import java.util.Scanner;
public class ExceptionExample {
public static int divide(int numerator, int denominator) {
return numerator / denominator;
}
public static void main(String[] args) {
int num1, num2, result;
System.out.println("Performing arithmetic operation->>");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the 1st number");
num1 = scanner.nextInt();
System.out.println("Enter the 2nd number");
num2 = scanner.nextInt();
//Call the divide function
result = divide(num1,num2);
System.out.println("Result: " + result);
}
}
Test Case -1
Performing arithmetic operation->>
Enter the 1st number
25
Enter the 2nd number
5
Result: 5
Test Case -2: If the user provides an input other than an integer value, an exception is raised and the program terminates abnormally as shown in the code snippet.
Performing arithmetic operation->>
Enter the 1st number
10
Enter the 2nd number
Hi
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at stringdemo.ExceptionExample.main(ExceptionExample.java:18)
Test Case -3: If the value of the denominator is zero.
Performing arithmetic operation->>
Enter the 1st number
10
Enter the 2nd number
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at stringdemo.ExceptionExample.divide(ExceptionExample.java:8)
at stringdemo.ExceptionExample.main(ExceptionExample.java:20)
To resolve the above problem and issue, you will use the exception handling mechanism using the try-catch block. Let’s apply the try-catch to the above code.
import java.util.Scanner;
public class ExceptionExample {
public static int divide(int numerator, int denominator) {
return numerator / denominator;
}
public static void main(String[] args) {
int num1, num2, result;
System.out.println("Performing arithmetic operation->>");
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the 1st number");
num1 = scanner.nextInt();
System.out.println("Enter the 2nd number");
num2 = scanner.nextInt();
//Call the divide function
result = divide(num1,num2);
System.out.println("Result: " + result);
scanner.close();
} catch (Exception e) {
System.out.println("Exception Caught: " + e);
}
}
}
A try block is used to monitor the user input in the above code. If the user enters an input that is not an integer or invalid value then an exception is raised and handled by a catch block.
However, the exception is successfully handled by the catch block and displays a message “Exception Caught” and the abnormal termination of the program is handled successfully.
The catch(Exception e), here Exception is a superclass and can manage all types of exception.
Test Case-1
Performing arithmetic operation->>
Enter the 1st number
10
Enter the 2nd number
0
Caught Exception: java.lang.ArithmeticException: / by zero
Test Case – 2
Performing arithmetic operation->>
Enter the 1st number
10
Enter the 2nd number
hi
Caught Exception: java.util.InputMismatchException
well explained
Nicely Explained
good
You must be logged in to submit a review.