0% found this document useful (0 votes)
38 views3 pages

throw and throws (1)

Uploaded by

ARYAN Shukla
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)
38 views3 pages

throw and throws (1)

Uploaded by

ARYAN Shukla
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/ 3

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. Java Exception Handling is a
mechanism to handle runtime errors such as ClassNotFoundException, IOException,
SQLException, RemoteException, etc.

In this article, we will learn about throw and throws in Java which can handle exceptions in Java.

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 in Java throw


throw Instance

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.

Java throw Examples


// Java program that demonstrates the use of throw
class ThrowExcep {
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.");
}
}
}

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

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.

Java throws Examples

// Java program to illustrate error in case


// of unhandled exception
class tst {
public static void main(String[] args)
{
Thread.sleep(10000);
System.out.println("Hello Geeks");
}
}

Output
error: unreported exception InterruptedException; must be caught or
declared to be thrown

Explanation
In the above program, we are getting compile time error because there is a chance of
exception if the main thread is going to sleep, other threads get the chance to execute
the main() method which will cause InterruptedException .

The differences between throw and throws in Java are:

Key
S.N. Difference throw throws

The throw keyword is used inside a The throws keyword is used in the
Point of function. It is used when it is function signature. It is used when the
1.
Usage required to throw an Exception function has some statements that can
logically. lead to exceptions.

The throws keyword can be used to


The throw keyword is used to declare multiple exceptions, separated
Exceptions
2. throw an exception explicitly. It can by a comma. Whichever exception
Thrown
throw only one exception at a time. occurs, if matched with the declared
ones, is thrown automatically then.

Syntax of throw keyword includes Syntax of throws keyword includes


the instance of the Exception to be the class names of the Exceptions to
3. Syntax thrown. Syntax wise throw be thrown. Syntax wise throws
keyword is followed by the keyword is followed by exception
instance variable. class names.

throw keyword cannot propagate


Propagation checked exceptions. It is only used
throws keyword is used to propagate
4. of to propagate the unchecked
the checked Exceptions only.
Exceptions Exceptions that are not checked
using the throws keyword.

You might also like