---
title: "What happens when the Java system detects that an Exception has been thrown?"  
description: "What happens when the Java system detects that an Exception has been thrown?"  
author: "Revati S Misra"  
published: 2023-07-28  
updated: 2023-07-29  
canonical: https://www.mindstick.com/forum/159342/what-happens-when-the-java-system-detects-that-an-exception-has-been-thrown  
category: "java"  
tags: ["exception handling", "java", "exception"]  
reading_time: 2 minutes  

---

# What happens when the Java system detects that an Exception has been thrown?

What happens when the [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [system](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business) detects that an [Exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) has been thrown?

## Replies

### Reply by Aryan Kumar

When the Java system detects that an exception has been thrown, it does the following:

1. **Determines the type of the exception.** The Java system uses the `instanceof` operator to determine the type of the exception.
2. **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.
3. **If a matching catch block is found, the Java system 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.
4. **If a matching catch block is not found, the Java system terminates the program.** This is because the Java system does not know how to handle the exception, and so it cannot continue executing the program.

The order of the catch blocks in a try-catch statement is important. The Java system 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 Java system will not search the remaining catch blocks.

Here are some additional details about what happens when the Java system detects that an exception has been thrown:

- 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/159342/what-happens-when-the-java-system-detects-that-an-exception-has-been-thrown

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
