日志文章

2019-12-19 aflfte2011

手动处理异常方法

package com.aflfte.exception;

public class Test04 {
public static void main(String[] args) {
Person p=new Person();
try {
p.setAge(-5);
} catch (ageException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

}
}
class Person{
int age;

public int getAge() {
return age;
}

public void setAge(int age) throws ageException {
if(age<0) {
throw new ageException("年龄不能小于0");
}
this.age = age;
}

}
class ageException extends Exception{
public ageException() {

}
public ageException(String msg) {
super(msg);
}
}

« 泛型的使用方法 | 编译器异常的处理方法»