---
title: "How to Handle Exception in java"  
description: "Try block is used to enclose the code that might throw exception. It must be used within method. Try block must be followed by either catch or finally"  
author: "Anonymous User"  
published: 2015-05-20  
updated: 2018-02-23  
canonical: https://www.mindstick.com/blog/10891/how-to-handle-exception-in-java  
category: "exception handling"  
tags: ["java", "exception", "error"]  
reading_time: 2 minutes  

---

# How to Handle Exception in java

Hi everyone in this blog I’m explaining about how to [handle the exception](https://www.mindstick.com/forum/159349/who-and-how-handle-the-exception-when-we-use-throws).\

**Java try-catch:**

Try block is used to enclose the code that might [throw an exception](https://www.mindstick.com/forum/159351/how-does-a-java-method-throw-an-exception-before-it-s-called). It must be used within the method. Try [block must be followed](https://www.mindstick.com/interview/2427/is-it-necessary-that-each-try-block-must-be-followed-by-a-catch-block) by either catch or [finally block](https://www.mindstick.com/articles/12106/exception-handling-in-java-guidelines-on-the-use-of-finally-block).

**The syntax of try-catch:**

try\

{\

//Code that may [throw exception](https://www.mindstick.com/forum/33937/difference-between-throw-exception-and-throw-clause)\

}

catch(Exception_Class_Name ref){}

**Syntax of try-finally:**

try\

{\
//Code that may throw exception\
}\
finally\
{\
//code that you want to execute in any condition\
}

**Java [catch block](https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block):**

Java catch block is used to handle the exception. It must be used after try block only. You can use [multiple catch](https://www.mindstick.com/forum/159236/explain-the-concept-of-multiple-catch-blocks-in-java-and-their-order-of-execution) block with a single try block.

**The problem without use [exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling):**

```
class Sample{     public static void main(String … s)     {            int d=10;            int r=d/0;            System.out.println(“Rest of the code….”);     }}
```

**Output:**

Exception in thread main java.lang.[ArithmeticException](https://www.mindstick.com/forum/159243/how-to-handle-the-arithmeticexception-in-java):/by zero

**Solution by use exception handling:**

```
class Sample{      public static void main(String … s)      {                  Try                  {                          int d=10;                          int r=d/0;                  }                  Catch(ArithmeticException ex)                  {                          System.out.println(“Rest of the code….”);                  }       }}
```

**Output:**

Exception in thread main java.lang.ArithmeticException:/by zero

Rest of the code….

\

Check more posts on exception handling [here](https://www.mindstick.com/Category/article/exception-handling#.VV3uVPmqqko)\

---

Original Source: https://www.mindstick.com/blog/10891/how-to-handle-exception-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
