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 com.sheting.basic.exception.throw_key.demo1;
public class InvalidAgeException extends Exception {
String message;
InvalidAgeException(String message) {
super(message);
this.message = message;
}
}
抛出异常
package com.sheting.basic.exception.throw_key.demo1;
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;
}
}
测试
package com.sheting.basic.exception.throw_key.demo1;
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);
}
}
运行结果:
com.sheting.basic.exception.throw_key.demo1.InvalidAgeException: Employee's age can not be less than 18
at com.sheting.basic.exception.throw_key.demo1.Employee.setAge(Employee.java:22)
at com.sheting.basic.exception.throw_key.demo1.EmployeeExceptionTest.main(EmployeeExceptionTest.java:11)
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 com.sheting.basic.exception.throw_key.demo2;
public class InvalidAgeException extends Exception {
String message;
InvalidAgeException(String message) {
super(message);
this.message = message;
}
}
抛出异常
package com.sheting.basic.exception.throw_key.demo2;
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;
}
}
测试
package com.sheting.basic.exception.throw_key.demo2;
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();
}
}
}
Run it result
com.sheting.basic.exception.throw_key.demo2.InvalidAgeException: Employee's age can not be less than 18
at com.sheting.basic.exception.throw_key.demo2.Employee.setAge(Employee.java:21)
at com.sheting.basic.exception.throw_key.demo2.EmployeeExceptionTest.main(EmployeeExceptionTest.java:13)
throw/throws区别
1.0 throw 在方法体中, throws在方法签名中.
2.0 throw抛出的异常,不需要try catch 处理, 而throws抛出的异常调用者必须处理.