Differentiate throws and throw keywords.
Both throw and throws are the concepts of exception handing in which throw is used to explicitly throw an exception from a method or any block of code while throws are used in the signature of the method to indicate that this method might throw one of the listed type exceptions.
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, which 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
The following are the important differences between throw and throws.
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.printIn("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.printIn("You shouldn't divide number by zero");
}
}
}
Output:
You shouldn't divide number by zero
OR,
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 where 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 where ‘throws’ keyword is used for declaration of exception, which indicates the exception that is thrown by the method.
- The ‘throw’ keyword could be utilised inside 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 where ‘throws’ keyword is used to propagate exception to the calling method. However, unchecked exception could be propagated by using throw keyword word.
- Another basis for the difference between the two is syntax. The syntax of ‘throw’ is followed by an instance variable but syntax of ‘throws’ is followed by the exception class names.
- ’Throw’ keyword is used within the method where ‘throws’ keyword is used with the method signature.
Comments
Post a Comment