throw和throws的使用
生活随笔
收集整理的這篇文章主要介紹了
throw和throws的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
throw和throws的使用
public class Person {private String name;private int age;public Person(){}Person(String name,int age){this.name = name;this.age = age;}public int getAge() {return age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void setAge(int age) throws Exception{if (age<1||age>120){throw new Exception("年齡錯誤,必須在1-120歲之間:"+age);}else {this.age = age;}}@Overridepublic String toString() {return "Person{name="+name+",age"+age+"}";}public static void main(String[] args) {Person person = new Person();person.setName("張三");try {person.setAge(340);}catch (Exception e){e.printStackTrace();}System.out.println(person);} }總結
以上是生活随笔為你收集整理的throw和throws的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php代理m3u8,PHP实现m3u8并
- 下一篇: java数组的扩容,将两个数组合并成一个