EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Top Differences Tutorial Throw vs Throws
 

Throw vs Throws

Chiranjan Saha
Article byChiranjan Saha
EDUCBA
Reviewed byRavi Rathore

Updated March 17, 2023

Throw-vs-Throws

 

 

Difference Between Throw and Throws in Java

An exception is an event which is happened during the execution of a program in java which is basically hampering the overall execution of the code. Sometimes system handles the exception by default, but in some cases, we need to handle exception based on our code or situations explicitly. Exception handing in customized fashion can be handled by using: try, catch, throw, throws and finally, keywords. When we want to define exception inside our code, we will use throws, whereas we will use throws when we want to define multiple exceptions explicitly. In this topic, we will discuss comparisons between throw vs throws.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is Throw?

When we want to handle any exception in our code that is unchecked, we will use throw. A throw can be used inside our code to handle exceptions in an explicit manner. Remember, we can only handle one type of exception at a time using throw. If you want to use for multiple using throw, you need to use throw that many times.

Syntax:

throw <exception_instance>

Example:

throw new ArithmeticException();

Those exception instances are required to be underclass called Throwable. This is because, Throwable has its subclass called Exception, and under the exception class, all of the user-defined exception classes reside. Therefore, make sure you are using exception instances which are subclasses under the line of throwable.

Now, let us understand the flow of the program when we use it to throw. The code will flow through until it reaches the throw, and after this, it will search for the try-catch block. The try block is examined to check whether there is any statement inside the catch block, which has mentioned an exception similar to the occurrence. If yes, then control is moved to that. If not, it will search for the next try-catch block, and this continues. Suppose it does not find any such try-catch block to handle an exception, then the system will handle exception using the default handler of exception, and the code will be terminated.

What is Throws?

When we assume that a method may show some exceptions in our code based on experience, we mention all of the method signature exceptions by using the throws keyword. All of the exceptions will be mentioned, separated by commas.

Syntax:

<method_type> <method_name> () throws <excpetion_name1>, <exception_name2>

Example:

void ExceptionExample() throws ArithmeticException, NullPointerException
{
//code
}

As you have got an idea of what throw and throws do, let us have a look into the differences between throw and throws as below:

Head to Head Comparison between Throw and Throws (Infographics)

Below are the top 5 differences between Throw vs Throws in Java

Throw-vs-Throws-info-1

Key Differences between Throw and Throws

Let us look at the key differences between Throw vs Throws in java as below:

  • When we want to define an exception inside our code, we will use throw. But, when we want to define exceptions explicitly, we will use throws.
  • If we use throw, code cannot flow in case of checked exceptions. When the exceptions are checked, it can be flowed using throws.
  • Syntax-wise, we write a variable, for instance, after throw. Whereas we write exception classes separated by commas after throws.
  • We use throw under a method definition. Whereas we declare throws exception in the signature of a method.
  • Throw has a limitation of handling the only exception at a time. On the other hand, Throws can handle multiple exceptions.

Throw vs Throws Comparison Table

Let’s discuss the top comparison between Throw vs Throws in Java.

Basis of Comparison 

Throw

Throws

How or When to Use In those cases, when we need to provide logical exception inside the code, we, in those cases, generally use the throw keyword under a function.

void ExceptionExample() throws ArithmeticException,
NullPointerException
{
throw
In those cases, when the statement of a function may result in an exception, we, in those cases, generally use throws keyword with a function to handle those exceptions.

void ExceptionExample() throws ArithmeticException,
NullPointerException
{
throw new NullPointerException ();
}
On the Basis of Exception Count At a single point in time, the throw can be used to throw only one exception. When we need to throw a particular exception, we will use throw.

For example:

// throw only a particular exception
throw new NullPointerException ();
Throws can handle multiple exceptions. When we need to handle multiple exceptions, we can use throws, where the name of the exceptions are mentioned with separated by a comma. When any exception happens, it will automatically match with the declared exceptions with throws and handle accordingly.

For example:

void ExceptionExample() throws ArithmeticException,
NullPointerException
{
throw new NullPointerException ();
}
On the Perspective of Syntax We use throw inside a function and to handle a single type of exception.

For example:

// throw only a particular exception
throw new NullPointerException ();
We will use throw with a function to handle multiple exceptions at one go by using the name of those exception classes.

For example:

void ExceptionExample() throws ArithmeticException,
NullPointerException
{
throw new NullPointerException ();
}
Checked/ Unchecked Perspective Checked exceptions cannot be handled by throw. It can handle only unchecked exceptions that cannot be handled by throws or checked exceptions.

For example:

void ExceptionExample() throws ArithmeticException,
NullPointerException
{
throw new NullPointerException (); //null pointer was not checked
, hence we used in throw
}
When we know the exception apriori and mention those in throws, those will become checked exceptions.

For example:

void ExceptionExample() throws ArithmeticException,
NullPointerException //explicit checked exceptions
{
//other code
}
Code Example Throw:

public class ExceptionExample {
public static void main(String[] args)
{
// Use of checked Exception by throw
try {
double num=1/0;
throw new ArithmeticException();
}
catch (ArithmeticException e)
{
e.printStackTrace();
}
}
}
Throws:

import java.io.IOException;
public class ExceptionExample {
public static void main(String[] args)
throws ArithmeticException //here we use unchecked
exception by throws
{
double num=1/0;
System.out.println (num);
}
}

Conclusion

These are differences between throw and throws in Java. Throws can give you freedom of using multiple exceptions at one go, but throw cannot. Primarily based upon this distinction, you must use throws if you need to provide multiple exceptions at one go; if you are not sure, then simply use throw to put exceptions one by one.

Recommended Articles

This is a guide to Throw vs Throws in Java. Here we discuss the key differences with infographics and comparison table. You can also go through our other suggested articles to learn more –

  1. Java List vs Array List
  2. JavaScript vs VBScript
  3. PHP vs JavaScript
  4. C vs Java

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW