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 throw, whereas when we want to define multiple exceptions explicitly, we will use throws. In this topic, we will discuss comparisons between throw vs throws
What is Throw?
When we want to handle any exception in our code which 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.
throw new ArithmeticException();
Those exception instances are required to be underclass called Throwable. This is because, Throwable has its subclass called Exception, and under 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 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 default handler of exception and 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 exceptions in the method signature by using throws keyword. All of the exceptions will be mentioned separated by commas.
<method_type> <method_name> () throws <excpetion_name1>, <exception_name2>
void ExceptionExample() throws ArithmeticException, NullPointerException
As you have got an idea 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 the 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
|How or When to Use||In those cases, when we need to provide logical exception inside the code, we, in those cases, generally, use 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 of time, the throw can be used to throw only one exception. When we need to throw a particular exception, we will use throw.
|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 the exception happens, it will automatically match with the declared exceptions with throws and handle accordingly.
|On the Perspective of Syntax||We use throw inside a function and to handle a single type of exception
|We will use throw with a function to handle multiple exceptions at one go by using the name of those exception classes.
|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.
|When we know the exception apriori and mention those in throws, those will become checked exceptions.
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.
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 –