---
title: "Exception Handling in Java"  
description: "Hi everyone in this article I am explaining how to handle exceptions in java"  
author: "Manoj Pandey"  
published: 2015-04-15  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1725/exception-handling-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Exception Handling in Java

##### Basically errors have two types

1. Compile Time Errors-: Compile times errors means errors of syntax. For example we have a method which take String parameter but we give us Integer parameter then it will be show compile time errors e.g.

```
Public void setData(String text{                     ---    }
```

// Call method

##### setData(123);

// Error because this method takes only String argument not an integer value.

2. Runtime Errors-: Runtime error means syntax are correct but errors in logical problems. For example

```
       int a[] = new int[2];System.out.println("Access element three :" + a[3]);
```

It will be throws exception ArrayIndexOutOfBoundException because in array object have only 2 index but we are getting value from third indexes so it will be throws exception.

Any [logical error](https://www.mindstick.com/forum/159519/explain-the-syntax-error-and-logical-error-classifications-in-c-providing-examples-of-each) which we can handle called exception.

An exception is a problem that arises during the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of a program. An exception can occur for many different reasons, including the following:

- A user has entered invalid data.
- A file that needs to be opened cannot be found.
- A [network connection](https://answers.mindstick.com/qa/94984/how-do-you-change-the-network-connection-priority-in-windows-10) has been lost in the middle of [communications](https://www.mindstick.com/articles/44489/leased-lines-and-other-tools-for-business-communications) or the JVM has run out of memory.

##### For example

[int value](https://www.mindstick.com/forum/159627/get-the-int-value-from-the-enum-in-c-sharp)=Integer.valueOf("1k");

int data type store only number or integer value it can’t store any [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) or String. So it will be throws runtime exception.

[Exception Handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) is a mechanism to [handle runtime](https://www.mindstick.com/forum/159700/how-to-handle-runtime-errors-in-a-dot-net-core-api) errors such as ClassNotFound, IO, SQL, Remote etc.

![Exception Handling in Java](https://www.mindstick.com/mindstickarticle/6c700801-f21e-49b8-9c09-c89d5b90d487/images/c5c1f137-6745-44fc-9c9b-4a4bb5b5b759.png)

For handle Exceptions use Try and catch. Add your code in try block and if any exception find then terminate your cursor from try block and then handle exception in the help of catch block. You can also add finally block which run every time if exception find or not.

##### Syntax-:

```
  public static void main(String args[])  {  try  {    type your code here.   }    catch (Exception ex     {  }    finally     {     Add your code for executed everytime     }     }
```

Java try block is used to enclose the code that might throw an exception. It must be used within the method.

Java try block must be followed by either catch or finally block.

Here a sample of exception handling

```
public class Sample {     public static void main(String args[]) {           int a[] = new int[2];           try {                 System.out.println("Access element three :" + a[3]);          } catch (Exception ex) {           } finally {                System.out.println("Finally block");           }      } }
```

It will throws exception ArrayOutOfBoundException but it handle by catch

\

block,finally block code will be exceuted

Output-: Finally block

---

Original Source: https://www.mindstick.com/articles/1725/exception-handling-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
