0% found this document useful (0 votes)
86 views4 pages

Throw and Throws in Java

Uploaded by

Ishan Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views4 pages

Throw and Throws in Java

Uploaded by

Ishan Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

throw and throws in Java

In Java, Exception Handling is one of the effective means to handle runtime errors so that the regular
flow of the application can be preserved. It handles runtime errors such as NullPointerException,
ArrayIndexOutOfBoundsException, etc. To handle these errors effectively, Java provides two key
concepts, throw and throws.

Java throw

The throw keyword in Java is used to explicitly throw an exception from a method or any block of
code. We can throw either checked or unchecked exception. The throw keyword is mainly used to
throw custom exceptions.

Syntax of throw in Java: throw Instance(Object)

Where instance is an object of type Throwable (or its subclasses, such as Exception).

Example: throw new ArithmeticException(“/ by zero”);

But this exception i.e., Instance must be of type Throwable or a subclass of Throwable.

The flow of execution of the program stops immediately after the throw statement is executed and
the nearest enclosing try block is checked to see if it has a catch statement that matches the type of
exception. If it finds a match, controlled is transferred to that statement otherwise next enclosing try
block is checked, and so on. If no matching catch is found then the default exception handler will
halt the program.

Example 1: This example demonstrates where an exception is thrown, caught, and re-thrown inside
a method.

// Java program to demonstrate

// how to throw an exception

class Geeks {

static void fun()

try {

throw new NullPointerException("demo");

catch (NullPointerException e) {

System.out.println("Caught inside fun().");

throw e; // rethrowing the exception

public static void main(String args[])

try {

fun();

catch (NullPointerException e) {

System.out.println("Caught in main.");

}
Output:

Caught inside fun().

Caught in main.

Java throws

throws is a keyword in Java that is used in the signature of a method to indicate that this method
might throw one of the listed type exceptions. The caller to these methods has to handle the
exception using a try-catch block.

Syntax of Java throws

type method_name(parameters) throws exception_list

where, exception_list is a comma separated list of all the exceptions which a method might throw.

In a program, if there is a chance of raising an exception then the compiler always warns us about it
and compulsorily we should handle that checked exception, Otherwise, we will get compile time
error saying unreported exception XXX must be caught or declared to be thrown. To prevent this
compile time error we can handle the exception in two ways:

1. By using try catch

2. By using the throws keyword

We can use the throws keyword to delegate the responsibility of exception handling to the caller (It
may be a method or JVM) then the caller method is responsible to handle that exception.

Example: Throw an exception if age is below 18 (print "Access denied"). If age is 18 or older, print
"Access granted":

public class Main {

static void checkAge(int age) throws ValidateException {

if (age < 18) {

throw new ValidateException("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...)

Important Points:

 throws keyword is required only for checked exceptions and usage of the throws keyword
for unchecked exceptions is meaningless.

 throws keyword is required only to convince the compiler and usage of the throws keyword
does not prevent abnormal termination of the program.

 With the help of the throws keyword, we can provide information to the caller of the
method about the exception.

You might also like