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());
            }
        
        }
    }
  • 相关阅读:
    将数据绑定通过图表显现
    d3.js初识
    Josn
    d3-tip.js
    Java知识点总结
    Javascript的学习
    Java的多线程学习
    day11
    day10
    day09
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5722126.html
Copyright © 2011-2022 走看看