Throw and throws in Java with example

Throw and throws in Java with example

throw and throws in Java are very important concept of exception handling. It is very useful to handle the both type of exceptions checked and unchecked. As we know that we can also handle exception without these keywords but if we use this keywords then we can generate personalized message and then show it to user.

In this tutorial we will see what is the different between throw and throws keyword in java and how it is useful in exception handling in Java.

You can also checkout my awesome powerful blog on java

There are two categories of exceptions in java

1). Unchecked Exceptions

This exception is checked at run time not compile time. an unchecked exception should be handled otherwise it will create run time error and terminate execution of the program. examples of unchecked exception: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.

2). Checked Exceptions

This exception is checked at compile time not run time. Examples of checked exceptions are IOException, InterruptedException, etc.

Throws in Java

Throws are used to declare an exception for a method. It helps to tell JVM that which type of exception can be thrown by that method. Particularly we use throws keyword with the method name followed by the class of exception name which can be thrown by that method. Generally, It works very similarly to the try-catch block.

Example of throws

[java]
public class Main{
int div(int a, int b) throws ArithmeticException{
int result = a/b;
return result;
}
public static void main(String args[]){
Main obj = new Main();
try{
System.out.println(obj.div(15,0));
}
catch(ArithmeticException e){
System.out.println(“We can’t divide any number by zero”);
}
}
}
[/java]

output

Throw in Java

A throw is used to throw its own generated exception with some customized message instead of the default. Suppose if something happens wrong then java sends default generalized message.

Now if we want to show our own user understandable message like your atm card has not insufficient money instead of generating any default message. It will help the user to understand the real scenario of what is going wrong.

Example of Throw

[java]
public class Main{
void validateAge(int age){
if(age<18)
throw new ArithmeticException(“You are Not Eligible for voting”);
else
System.out.println(“You are Eligible for voting”);
}
public static void main(String args[]){
Main obj = new Main();
try {
obj.checkAge(13);
} catch(ArithmeticException e) {
e.printStackTrace();
}
}
}
[/java]

Difference between throws and throw

1). Throws are used to declare the type of exception which is possibly generated by that method But the throw is used to throw an exception explicitly to protect the program from abnormal termination and show user understandable message.

2). Throws is followed by the exception class name and throw is followed by the object of the exception class.

3). Using throw keyword you can throw only one exception at a time but Using throws we can declare multiple exceptions at a time and can handle them.