In this tutorial, we are going to see difference between throw and throws in java.
throw:
throw keyword is used to throw any custom exception or predefine exception.
For example:
Let’s say you want to throw invalidAgeException when employee age is less than 18.
Create a Employee class as below.
package org.arpit.java2blog;
public class Employee {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age < 18)
try {
throw new InvalidAgeException("Employee's age can not be less than 18");
} catch (InvalidAgeException e) {
e.printStackTrace();
}
this.age = age;
}
}
Create InvalidAgeException class as below
package org.arpit.java2blog;
public class InvalidAgeException extends Exception{
String message;
InvalidAgeException(String message)
{
super(message);
this.message=message;
}
}
Now create a main class named EmployeeExceptionTest.java as below.
package org.arpit.java2blog;
public class EmployeeExceptionTest {
public static void main(String[] args) {
Employee e1 = new Employee();
e1.setName("John");
e1.setAge(25);
Employee e2 = new Employee();
e2.setName("Martin");
e2.setAge(17);
}
}
when you run above program, you will get below output:
org.arpit.java2blog.InvalidAgeException: Employee's age can not be less than 18 at org.arpit.java2blog.Employee.setAge(Employee.java:19) at org.arpit.java2blog.ExceptionTest.main(ExceptionTest.java:14)
throws:
throws keyword is used to declare list of all exception which method might throw. It delegates responsibility of handling exception to calling method.
For example:
Let's say you want to declare InvalidAgeException in setAge() method when employee age is less than 18 and InvalidAgeException exception should be handled in main method.
Create a Employee class as below.
package org.arpit.java2blog;
public class Employee {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws InvalidAgeException{
if(age < 18)
throw new InvalidAgeException("Employee's age can not be less than 18");
this.age = age;
}
}
Create InvalidAgeException class as below
package org.arpit.java2blog;
public class InvalidAgeException extends Exception{
String message;
InvalidAgeException(String message)
{
super(message);
this.message=message;
}
}
Now create a main class named EmployeeExceptionTest.java as below.
package org.arpit.java2blog;
public class EmployeeExceptionTest {
public static void main(String[] args) {
try {
Employee e1 = new Employee();
e1.setName("John");
e1.setAge(25);
Employee e2 = new Employee();
e2.setName("Martin");
e2.setAge(17);
} catch (InvalidAgeException e) {
e.printStackTrace();
}
}
}
when you run above program, you will get below output:
org.arpit.java2blog.InvalidAgeException: Employee's age can not be less than 18 at org.arpit.java2blog.Employee.setAge(Employee.java:19) at org.arpit.java2blog.ExceptionTest.main(ExceptionTest.java:14)
If you notice, we have used throws keyword in Employee's setAge method instead of handling InvalidAgeException.
public void setAge(int age) throws InvalidAgeException{
if(age < 18)
throw new InvalidAgeException("Employee's age can not be less than 18");
this.age = age;
}
Now we have used try catch block in main method to handle InvalidAgeException.