In this guide, we will discuss the difference between throw and throws keywords. Before going though the difference, refer my previous tutorials about throw and throws.
Throw vs Throws in java
1. Throws clause is used to declare an exception, which means it works similar to the try-catch block. On the other hand throw keyword is used to throw an exception explicitly.
2. If we see syntax wise than throw is followed by an instance of Exception class and throws is followed by exception class names.
For example:
throw new ArithmeticException("Arithmetic Exception");
and
throws ArithmeticException;
3. Throw keyword is used in the method body to throw an exception, while throws is used in method signature to declare the exceptions that can occur in the statements present in the method.
For example:
Throw:
... void myMethod() { try { //throwing arithmetic exception using throw throw new ArithmeticException("Something went wrong!!"); } catch (Exception exp) { System.out.println("Error: "+exp.getMessage()); } } ...
Throws:
... //Declaring arithmetic exception using throws void sample() throws ArithmeticException{ //Statements } ...
4. You can throw one exception at a time but you can handle multiple exceptions by declaring them using throws keyword.
For example:
Throw:
void myMethod() { //Throwing single exception using throw throw new ArithmeticException("An integer should not be divided by zero!!"); } ..
Throws:
//Declaring multiple exceptions using throws void myMethod() throws ArithmeticException, NullPointerException{ //Statements where exception might occur }
These were the main differences between throw and throws in Java. Lets see complete examples of throw and throws keywords.
Throw Example
To understand this example you should know what is throw keyword and how it works, refer this guide: throw keyword in java.
public class Example1{ void checkAge(int age){ if(age<18) throw new ArithmeticException("Not Eligible for voting"); else System.out.println("Eligible for voting"); } public static void main(String args[]){ Example1 obj = new Example1(); obj.checkAge(13); System.out.println("End Of Program"); } }
Output:
Exception in thread "main" java.lang.ArithmeticException: Not Eligible for voting at Example1.checkAge(Example1.java:4) at Example1.main(Example1.java:10)
Throws Example
To understand this example you should know what is throws clause and how it is used in method declaration for exception handling, refer this guide: throws in java.
public class Example1{ int division(int a, int b) throws ArithmeticException{ int t = a/b; return t; } public static void main(String args[]){ Example1 obj = new Example1(); try{ System.out.println(obj.division(15,0)); } catch(ArithmeticException e){ System.out.println("You shouldn't divide number by zero"); } } }
Output:
You shouldn't divide number by zero
prahalad says
i m not understand the difference between throw and throws..plz help me..
Chaitanya Singh says
Hi Prahalad, Could you please let me know in which part you faced difficulties to understand? Try to go through the separate articles of throw keyword and throws clause and then go through the differences, you will be able to understand better then. Thanks!
Hemanth Kumar says
By using Throw keyword in java you cannot throw more than one exception but using throws you can declare multiple exceptions.
I hope this solves your confusion.
Jagadeesh Verri says
You should have explained the flow of execution when any program is given as a supporting example, that would make things easier and comfortable
fazeela says
perfect explanation
just what i was looking for
Thank you
chandan says
Sir, If we have only one Exception then which one better option to use (throw or throws)…….?
Jamil says
Single exception = Throw
Multiple Exceptions = Throws
Chaitanya Singh says
Actually the usage of throw and throws is completely different. It depends whether you are throwing an exception or handling it. To throw an exception we use throw keyword while to handle (catch) an exception we use throws clause.
gajanan says
Give with example how to handle exception with throws keyword
Nurseyit says
yes you are right!!
Kartik Sagar says
Actually Throws keyword is used to throw the checked exceptions which a programmer doesn’t want to handle it so a programmer throws these exception so that compiler did not give an error but throw keyword is used to throw an built-in-exception or user defined exception explicitly so both concepts are different so go through these concepts separately and then read these differences then you will did not get any confusion.
Sanjay says
throws is best for both single and multiple exceptions.
Mayur says
Thank You for the useful info .
phally says
how many types of exception ?
Chaitanya Singh says
There are two types of exception: checked and unchecked. You can refer this article: https://beginnersbook.com/2013/04/java-checked-unchecked-exceptions-with-examples/
Dharam Jaswal says
Realy very…..nice tutorial by you
Thanks
sarika says
Iam not getting why we use new keyword with throw……why there is need for this declaration?
can any1 explain me?
Chaitanya Singh says
Refer the reply to Priya’s comment.
priya says
why we declare -throw new exception?
Chaitanya Singh says
@Sarika and @Priya: the statement throw new Exception(“Something went wrong!!”); in the above program is throwing a custom exception with our own defined message “Something went wrong!!”. It is just an example to show that the code in try block is actually throwing an exception. Refer this article to learn more about throwing a custom exception: https://beginnersbook.com/2013/04/user-defined-exception-in-java/
Amit says
What happens if you don’t call wait and notify from synchronized block?
ashfaq says
sir , if we can handle all the exception by mentioning THROWS key word in main() method, then whats the use of throw, or why should we go individually to all the methods.?
rupali says
in above comment u r saying “To throw an exception we use throw keyword while to handle (catch) an exception we use throws clause.”,but can u explain me 1 doubt ,although throw is an explicit creator,but still we have to handle that exception or that method using try catch where in that method we write throw new any …exception.
and when we using throws clause that time also we have to handle that exception or method throwing an exception.
that means in BOTH cases we are HANDLING the exception,is it right or not?
ramesh says
how can we handle the exceptions without using catch,throw, and throws clause.
I faced this question in one interview. kindly reply me….
Vinodha g says
@ramesh.. If u get the answer Please answer me to this question.
shalini Gupta says
by using try and finally only, we can handle an exception without using catch throw and throws.
Deepa says
This is very useful. Good job on documenting these differences so well.! Thanks.
chandrakanth.B says
hi sir, can u send still more java interview questions so that we will easy to face the any interviews.thanq…………
Chaitanya Singh says
Hello chandrakanth.B, Refer this article: https://beginnersbook.com/2013/05/java-interview-questions/
bindhu says
i understood the throw and throws difference. i want what is what and simple explanation.
Chaitanya Singh says
I have provided the links of the detailed articles of throw and throws keyword at the beginning of this post. Please refer them and let me know if you have any questions.
sindhuja says
what is the difference between exception and error?
Vinodha g says
Error is a compile time error it may be syntax error or else.. But exception occurs during the execution time it cannot get identified by compiler.
shradha patel says
Does throw use for checked or unchecked exceptions………????
Please let me know I am so confused.
hemavathi says
class A{
public void add()throws IOException
{
…….
}
class B extends A
{
public void add()throws Exception
{
………….
}
}
can we take like this?
please explain me if we take like this what happen I faced this question in one interview.
Prashant says
@Hemavati, The program you written above is not possible because
in ur prgrm the add() method is overrided right… Then overrided method should not change its declaration in any sub classes.. der Add() method id declared with the diff exception classes so it cannot possible…(throws different Exception ).
But…
We can declare different Exception with method body… only when Superclass method is having super Exception class and subclass has subclass of exception,,
in your program Exception is super class and the IOexception is Sub class of Exception… hence Your program can rewritten like…
class A{
public void add()throws Exception
{
…….
}
class B extends A
{
public void add()throws IOException
{
………….
}
}
Hope you got my msg….
Arvind singh says
Is it possible to handle the exception without using try-catch block and throws?..i am so confused bz this question was asked by interviewer….please help me.
Arvind singh says
One more difference between throws and throw?
Throw is associated with try-catch block whereas throws is declared with method declaration..
if there is any mistakes…please respond me..thanks
dheer says
I have a confusion, which of these two (throw and throws) are used to trigger any exception.
Durgesh says
when use to throw and throws ..?
Tarang says
using throw one can handle custom exception
rajesh says
if there is a catch block to handle the exceptions then what is the need of throws block
Nawal Sah says
I want to execute a few stmts (lets say stmt1,stmt2) which raise a checked exception(lets say BadException1). I want to execute stmt3,stmt4 no matter whether stmt1 and stmt2 fail or not. Also stmt3,stmt4 throws checked exception BadException2. Write a code block to demonstrate how you will ensure this.
Shubham says
Is there any way to convert checked exception into unchecked ?
Farhana says
when writing IO programs it forces to handle IOException at that time best way is to use throws clause.