Java Throw

Java Throw Keyword

Java Throw

Sometimes you want to throw your own exception at some particular time. Throw keyword in java exception allows you to explicitly throw an exception at run time. You can throw any exception with your own exception details.

In this tutorial, we will discuss about throw keyword and how you can explicitly throw an exception in your java program.

Java Throw Keyword

Throw Keyword in java

Java throw keyword allows you to throw any exception whether it is checked or unchecked exception at run time in java. You can provide your own exception details when you throw an exception.

Basic syntax

throw new ExceptionClass("Exception details");

Let’s create a basic example on how we can throw an exception in our java program.

class ThrowExceptionDemo {

  public static void main(String arg[]) {

     try {
        int a = 9;

        if(a < 10) {
             throw new Exception("A is less than the value of 10.");
     } catch (Exception e) {
        System.out.println("Exception : " + e.getMessage());


output :
Exception : A is less than the value of 10.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *