‘Throw’ and ‘throws’ would seem similar in general life having a difference of tenses only. However, in the programming language, Java, these two are very different from each other and are used to do various tasks. Throw and Throws are keywords in Java used in exception handling.
Use of Throw and Throws in Java
The ‘Throw’ keyword is used to give an instance of exception that the programmer has manually created to JVM whereas the ‘throws’ keyword is used to give the responsibilities of exception handling, that occurred in the method to the caller method.
Syntax-wise: In throw instance of exception is defined in the exception block/class. In Throws, the throw keyword is followed by the exception class
Throw vs Throws in Java
THROW |
THROWS |
A throw is used to throw an exception explicitly |
A throws to declare one or more exceptions, separated by commas. |
Can throw a single exception using throw |
Multiple can be thrown using Throws |
This keyword is used in the method |
A signature method is used with keyword throws |
Only unchecked exceptions propagated using the throw keyword. |
To raise an exception throws keyword followed by the class name and a checked exception can be propagated. |
Throw keyword is followed by the instance variable |
Throws keyword is followed by the exception class |
What is Throw in Java?
Throw keyword in Java is used to throw an exception explicitly and logically defined by the programmer during the program control moves from one block to another assuming the exception of the error are defined and handled accordingly.
Throw Syntax in Java
syntax of throw :- throw <instance>;
Throw Example in Java
void mtod (){
throw new mathsexception(“we are sorry there is no solution”);
}
Program :
public class ThrowExample{
void Votingage(int age){
if(age<18)
throw new ArithmeticException("you can't vote as not Eligible to vote");
else
System.out.println("Eligible for voting");
}
public static void main(String args[]){
ThrowExample obj = new ThrowExample();
obj.Votingage(13);
System.out.println("End Of Program");
}
}
OUTPUT
$java -Xmx128M -Xms16M ThrowExample
Exception in thread "main" java.lang.ArithmeticException: you can't vote as not Eligible to vote at ThrowExample.Votingage(ThrowExample.java:5)at ThrowExample.main(ThrowExample.java:11)
What is Throws in Java?
Throws in Java is used to declare and call an exception block, which means its working is similar to the try-catch block.
Throws Example in Java
public class ThrowsExample{
int divion(int a, int b) throws ArithmeticException{
int intet = a/b;
return intet;
}
public static void main(String args[]){
ThrowsExample obj = new ThrowsExample();
try{
System.out.println(obj.divion(15,0));
}
catch(ArithmeticException e){
System.out.println("Division cannot be done using ZERO");
}
}
}
Output
$java -Xmx128M -Xms16M ThrowsExample
Division cannot be done using ZERO
Key difference between Throws and Throw in Java
✓ The basic difference between these two terms is that throws keyword uses the name of the exception classes whereas the throw keyword uses the exception object.
✓The throw keyword can throw only one i.e. a single exception instance. On the other hand, throws keyword can throw multiple exception classes and separate by a comma.
✓The throw keyword is used to simply throw an exception whereas throws keyword is used for declaration of exception, which indicates the exception that is thrown by the method.
✓The throw keyword could be utilized inside the method or static block initializer. The throws, on the other hand, could only be used in method declaration.
✓The throw keyword is unable to propagate the unchecked exception to the calling method whereas the throws keyword can propagate exception to the calling method. However, unchecked exceptions could be propagated by using throw keyword words.
✓Another basis for the difference between the two is syntax. The throw syntax is followed by an instance variable but the syntax of throws is followed by the exception class names.
✓Throw keyword is used within the method whereas throws keyword is used with the method signature.