zoukankan      html  css  js  c++  java
  • 使用throw抛出年龄异常

    package com.oned9z;
    /**
    * @program: com.oned9z
    * @description:使用throw抛出年龄异常
    * @author: Mr.Lin
    * @create: 2019年7月23日
    **/
    public class Person {
    private int age;

    public int getAge() {
    return age;
    }

    public void setAge(int age) throws Exception{
    if(age>=1 && age<=100){
    this.age = age;
    }else{
    throw new Exception("年龄必须在1—100之间!");
    }

    }

    }

    package com.oned9z;
    /**
    * @program: com.oned9z
    * @description:测试类
    * @author: Mr.Lin
    * @create: 2019年7月23日
    **/
    public class TestAge {
    public static void main(String[] args) {
    Person person = new Person();
    try{
    person.setAge(1231312);
    }catch (Exception e){
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    bzoj 3924
    bzoj 1095
    luogu 4886
    bzoj 2152
    CF960G
    bzoj 3561
    bzoj 4176
    bzoj 4407
    bzoj 3309
    luogu 4608
  • 原文地址:https://www.cnblogs.com/lpbk/p/11233575.html
Copyright © 2011-2022 走看看