# Exception handling in Java

An exception is an unwanted or unexpected event that occurs during the execution of the program that disrupts the normal flow.

```java
class Test
{
    public static void main(String[] args)
    {
        System.out.println("1");
        System.out.println("2");
        System.out.println("3");
        System.out.println("4");
        System.out.println("5");
        System.out.println(10/0);
        System.out.println("6");
        System.out.println("7");
    }
}
```

Output is printed only upto 5 and an arithmetic exception is shown

## Difference between exception and error

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674126637208/769b6d06-0bda-4018-a839-bc4e14f82fa5.png align="center")

## Hierarchy of Exception

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674126688069/0a0a7b8d-10ce-47b8-874a-62b702baa9a2.png align="center")

There are two types of exceptions in Java

* Checked exception/Compile time exception
    
* Unchecked exception/Run time exception
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674136659205/2aade326-8eb0-4b1b-8519-0cc04908ae2e.png align="center")

## Exception keywords in Java

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674233686886/90366af5-e380-4339-a970-de907c737f3b.png align="center")

Whenever there is an exception, the method in which it occurs will create an object with the following properties:

* exception name
    
* description
    
* stack trace
    

We can get the exception as output using

1. e.printStackTrace();
    
    > prints exception name, stack trace and description
    
2. SOP(e); // SOP(e.toString());
    
    > prints exception name and description
    
3. SOP(e.getMessage());
    
    > prints only decription
    

### 1\. Try catch

```java
//syntax
try
{
    //risky code
}
catch(ExceptionClassName ref.var.name)
{
    //handling code
}
```

```java
class Test
{
    public static void main(String[] args)
    {
        try
        {
            int a=10,b=0,c;
            c=a/b;
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
```

## 2\. Finally

Finally is the block that is always executed whether an exception is handled or not

```java
class Test
{
    public static void main(String[] args)
    {
        try
        {
            int a=10,b=0,c;
            c=a/b;
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            System.out.println("finally block");
        }
    }
}
```

## 3\. Throw

```java
public class Main {
  static void checkAge(int age) {
    if (age < 18) {
      throw new ArithmeticException("Access denied - You must be at least 18 years old.");
    }
    else {
      System.out.println("Access granted - You are old enough!");
    }
  }

  public static void main(String[] args) {
    checkAge(15); // Set age to 15 (which is below 18...)
  }
}
```

## 4\. Throws

```java
public class Main {
  static void checkAge(int age) throws ArithmeticException {
    if (age < 18) {
      throw new ArithmeticException("Access denied - You must be at least 18 years old.");
    }
    else {
      System.out.println("Access granted - You are old enough!");
    }
  }

  public static void main(String[] args) {
    checkAge(15); // Set age to 15 (which is below 18...)
  }
}
```
