- 关于异常对象的产生
- 系统自动生成的异常对象
- 手动生成一个异常对象,并抛出(throw)
package p8exception.p9;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
try {
s.regist(-1001);
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Student {
private int id;
public void regist(int id) throws Exception {
if(id > 0) {
this.id = id;
}else {
// System.out.println("非法");
// throw new RuntimeException("输入非法");
throw new Exception("输入非法");
}
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
'}';
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)