---
title: "What is the internal behavior of exception handling?"  
description: "What is the internal behavior of exception handling?"  
author: "Revati S Misra"  
published: 2023-07-28  
updated: 2023-07-29  
canonical: https://www.mindstick.com/forum/159345/what-is-the-internal-behavior-of-exception-handling  
category: "java"  
tags: ["exception handling", "java", "exception"]  
reading_time: 2 minutes  

---

# What is the internal behavior of exception handling?

What is the [internal](https://www.mindstick.com/interview/773/describe-the-accessibility-modifier-protected-internal) [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) of [exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling)?

## Replies

### Reply by Aryan Kumar

The internal behavior of [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) [handling](https://www.mindstick.com/forum/34585/file-handling) in Java is as follows:

1. When an exception is thrown, the Java Virtual Machine (JVM) searches the call stack for a matching catch block. 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.
2. 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.
3. 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 handle an exception:

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 the internal behavior of exception handling:

- 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/159345/what-is-the-internal-behavior-of-exception-handling

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
