---
title: "How to use Try and Catch in Java for Exception handling and what happens when an exception occurs?"  
description: "How to use Try and Catch in Java for Exception handling and what happens when an exception occurs?"  
author: "Revati S Misra"  
published: 2023-07-28  
updated: 2023-07-29  
canonical: https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs  
category: "java"  
tags: ["exception handling", "java", "exception"]  
reading_time: 3 minutes  

---

# How to use Try and Catch in Java for Exception handling and what happens when an exception occurs?

How to use Try and [Catch](https://www.mindstick.com/forum/159344/java-exceptions-and-catch-clause) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) for [Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) and what happens when an exception occurs?

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that.

In Java, `try` and `catch` are two keywords that are used to handle exceptions. A `try` block is used to enclose the code that could potentially throw an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling). A `catch` block is used to handle the exception that is thrown by the `try` block.

The syntax for a `try-catch` block is as follows:

```plaintext
try {
  // Code that could potentially throw an exception
} catch (Exception e) {
  // Code that handles the exception
}
```

The `try` block contains the code that could potentially throw an exception. The `catch` block is used to handle the exception. The `Exception` keyword is a placeholder for the type of exception that can be caught.

For example, the following code uses a `try-catch` block to handle the `ArithmeticException` exception:

```plaintext
try {
  int x = 10;
  int y = 0;
  int z = x / y;
} catch (ArithmeticException e) {
  System.out.println("Division by zero");
}
```

In this code, the `try` block contains the code that could potentially throw an `ArithmeticException` exception. If the `y` variable is equal to 0, then the `ArithmeticException` exception will be thrown. The `catch` block will then be executed, and the message "Division by zero" will be printed.

If the `y` variable is not equal to 0, then the `ArithmeticException` exception will not be thrown. In this case, the `try` block will execute normally, and the `catch` block will be skipped.

When an exception occurs, the following happens:

1. The Java Virtual Machine (JVM) determines the type of the exception.
2. The JVM searches the call stack for a matching catch block.
3. If a matching catch block is found, the JVM transfers control to the catch block.
4. The catch block handles the exception, which may involve logging the exception, displaying an error message, or terminating the program.
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 to use `try` and `catch` in Java for exception [handling](https://www.mindstick.com/forum/34585/file-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/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
