Throw keyword in Java

In this tutorial, we will learn about throw keyword in Java. The throw keyword in java is used to throw an exception explicitly. We can throw both kinds of checked and unchecked exceptions using this throw keyword. But we mainly use throw keyword to throw custom exceptions(also known as the user-defined exceptions). Check out the following syntax of throw keyword.

throw objectOfExceptionClass;  

As in above syntax you can see to throw an exception first of all we have to write throw keyword then object of Exception class. Please check out the following example program.

Example Program:

public class Example
    public static void main(String args[]) 
            System.out.println("In try block");
            throw new ArithmeticException();
        catch(ArithmeticException e) 
            System.out.println("In catch block");


In try block
In catch block


Spread the love:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share

Great News! Now you can read all the tutorials on our Android App..

Download Owlbuddy: Learn Programming Tutorials App Now.

Download Owlbuddy: Learn Programming Tutorials