---
title: "How does an exception find the correct catch block?"  
description: "How does an exception find the correct catch block?"  
author: "Utpal Vishwas"  
published: 2023-07-28  
updated: 2023-07-29  
canonical: https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block  
category: "java"  
tags: ["exception handling", "exception"]  
reading_time: 2 minutes  

---

# How does an exception find the correct catch block?

How does an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) find the correct [catch block](https://www.mindstick.com/forum/159240/how-to-handle-multiple-exceptions-using-the-catch-block-in-java)?

## Replies

### Reply by Aryan Kumar

When an exception is thrown, the Java Virtual Machine (JVM) searches the call stack for a matching [catch](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs) [block](https://www.mindstick.com/forum/157599/explain-the-process-control-block-pcb-in-the-operating-system). The call stack is a record of the methods that have been called, and it is used to determine which method should handle the exception.

The JVM starts at the top of the call stack and searches for a catch block that matches the type of the exception that was thrown. If a matching catch block is found, the JVM transfers control to the catch block. The catch block then handles the exception, which may involve logging the exception, displaying an error message, or terminating the program.

If a matching catch block is not found, the JVM continues to search the call stack. If the JVM reaches the bottom of the call stack and still has not found a matching catch block, the JVM terminates the program.

The following steps are taken by the JVM to find the correct catch block:

1. The JVM identifies the exception that was thrown.
2. The JVM searches the call stack for a catch block that matches the type of the exception.
3. If a matching catch block is found, the JVM transfers control to the catch block.
4. The catch block handles the exception.
5. If a matching catch block is not found, the JVM terminates the program.

The order of the catch blocks in a try-catch statement is important. The JVM will only search the catch blocks in the order that they are declared. If the correct catch block is not found in the first catch block, the JVM will not search the remaining catch blocks.

Here are some additional details about how an exception finds the correct catch block:

- The catch block must match the type of the exception that was thrown.
- The catch block must be declared in the same method or class as the code that threw the exception.
- The catch block must be declared before the code that throws the exception.


---

Original Source: https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
