zoukankan      html  css  js  c++  java
  • throws 和 throw

    package unit5;
    
    public class Person {
        private int age;
        private String name;
        public String getName() {
            return name;
        }
        public int  getAge() {
            return age;
        }
        public void setAge(int a) throws Exception {
            if(a<0){
                throw new Exception("出现异常,年龄不能小于零");
            }
            if(a>1000){
                throw new Exception("出现异常,年龄不能大于1000");
            }
            age = a;
        }
        public void setName(String name) {
            this.name=name;
        }
        public void display() {
            System.out.println("我叫 "+name+"今年 "+age+" 岁。");
        }
    
    }
    package unit5;
    
    public class Throwsdeml {
        public static void main(String[] args) {
    
            Person zhang = new Person();
            try{
                zhang.setName("张三");
                zhang.setAge(2000);
                zhang.display();
            }catch (Exception e) {
    
                System.out.println(e.getMessage());
            }
        
        }
    }
  • 相关阅读:
    xadmin 安装
    Python
    使用免费证书安装 ipa 到真机
    Jupyter PPT
    Javascript注意点
    论文哪里找?
    神经网络术语
    PyTorch上路
    DCGAN实现
    数据库和数据挖掘领域的会议和期刊
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5722126.html
Copyright © 2011-2022 走看看