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.
Page Contents
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[])
{
try
{
System.out.println("In try block");
throw new ArithmeticException();
}
catch(ArithmeticException e)
{
System.out.println("In catch block");
}
}
}
Output:
In try block
In catch block
Parvesh Sandila is a results-driven tech professional with 8+ years of experience in web and mobile development, leadership, and emerging technologies.
After completing his Master’s in Computer Applications (MCA), he began his journey as a programming mentor, guiding 100+ students and helping them build strong foundations in coding. In 2019, he founded Owlbuddy.com, a platform dedicated to providing free, high-quality programming tutorials for aspiring developers.
He then transitioned into a full-time programmer, where his hands-on expertise and problem-solving skills led him to grow into a Team Lead and Technical Project Manager, successfully delivering scalable web and mobile solutions. Today, he works with advanced technologies such as AI systems, RAG architectures, and modern digital solutions, while also collaborating through a strategic partnership with Technobae (UK) to build next-generation products.
