---
title: "What is the use of the throw keyword to manually throw an exception in Java?"  
description: "What is the use of the throw keyword to manually throw an exception in Java?"  
author: "Steilla Mitchel"  
published: 2023-07-23  
updated: 2023-07-24  
canonical: https://www.mindstick.com/forum/159239/what-is-the-use-of-the-throw-keyword-to-manually-throw-an-exception-in-java  
category: "java"  
tags: ["exception handling", "javascript"]  
reading_time: 3 minutes  

---

# What is the use of the throw keyword to manually throw an exception in Java?

[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the use of the throw [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) to manually throw an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) in Java.

## Replies

### Reply by Aryan Kumar

The throw keyword is used to manually throw an exception in Java. It can be used to throw any type of exception, including checked and unchecked exceptions.

The general syntax for using the throw keyword is as follows:

```plaintext
throw exception_object;
```

where exception_object is an object of an exception class.

Here is an example of how to use the throw keyword to throw an ArithmeticException:

```plaintext
public class ThrowKeywordExample {

  public static void main(String[] args) {
    try {
      int number1 = 10;
      int number2 = 0;

      int result = number1 / number2;
    } catch (ArithmeticException e) {
      System.out.println("An error occurred: " + e.getMessage());
    } finally {
      System.out.println("This code will always execute");
    }
  }
}
```

In this example, the try block contains the code that may throw an ArithmeticException. If the ArithmeticException is thrown, it will be caught by the catch block. The catch block then prints a message to the console indicating that an error occurred.

The finally block is executed regardless of whether or not an exception is thrown. In this example, the finally block prints the message "This code will always execute" to the console.

The throw keyword can also be used to throw custom exceptions. Custom exceptions are exceptions that you create yourself. To create a custom exception, you need to create a class that extends the Exception class.

Here is an example of how to create a custom exception:

```plaintext
public class MyException extends Exception {

  private String message;

  public MyException(String message) {
    this.message = message;
  }

  public String getMessage() {
    return this.message;
  }
}
```

This class defines a custom exception called MyException. The MyException class extends the Exception class, and it has a single property called message. The message property is a string that contains the message that will be displayed when the exception is thrown.

To throw a custom exception, you need to create an object of the custom exception class and then throw it using the throw keyword.

Here is an example of how to throw a custom exception:

```plaintext
public class ThrowCustomExceptionExample {

  public static void main(String[] args) {
    try {
      // Code that may throw an exception
    } catch (MyException e) {
      System.out.println("An error occurred: " + e.getMessage());
    } finally {
      System.out.println("This code will always execute");
    }
  }
}
```

In this example, the try block contains the code that may throw an MyException. If the MyException is thrown, it will be caught by the catch block. The catch block then prints a message to the console indicating that an error occurred.

The message property of the MyException object is set to the string "This is an error message". This message will be displayed when the exception is thrown.

The finally block is executed regardless of whether or not an exception is thrown. In this example, the finally block prints the message "This code will always execute" to the console.


---

Original Source: https://www.mindstick.com/forum/159239/what-is-the-use-of-the-throw-keyword-to-manually-throw-an-exception-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
