Updated March 17, 2023
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.
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
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.
|
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.
|
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:
|
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:
|
On the Perspective of Syntax | We use throw inside a function and to handle a single type of exception.
For example:
|
We will use throw with a function to handle multiple exceptions at one go by using the name of those exception classes.
For example:
|
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:
|
When we know the exception apriori and mention those in throws, those will become checked exceptions.
For example:
|
Code Example | Throw:
|
Throws:
|
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 –