Difference between throw and throws in java

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抛出的异常调用者必须处理.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值