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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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:
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 |
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.