异常练习一 throw

package 异常练习;
class OutageroudleException extends RuntimeException{
 OutageroudleException(){
 }
 OutageroudleException(String Message){
  super(Message);//向上传递给被它继承的父类
 }
}
class Person{ 
 private String name;
 private int age;
 Person(String name,int age){//初始化这个类
  this.name=name;
  this.age=age;
  if(age<0||age>150){
   throw new OutageroudleException(age+"年龄出界了");//抛出一个自定义好的异常类的对象
  }
 }
 public String toString(){
  return "姓名"+name+","+"年龄"+age;
 }
}
public class ExceptionDmeo {
 public static void main(String[] args){
  Person per=new Person("小明",-20);
  System.out.println(per.toString());
 }
}

原文地址:https://www.cnblogs.com/daoxiang1992/p/5682828.html