public class TestSignIn {
public static void main(String[]args){
TestSignIn t=new TestSignIn();
student s=new student();
try {
s.login(-1001);
}catch(Exception e){//处理此异常
System.out.println(e.getMessage());
//通过调用getMessage来输出写入的信息
}
}
}
class student{
private int id;
public void login(int id) throws Exception{//抛出异常
if(id>0){
this.id=id;
System.out.println(this.id);
}
else{
throw new Exception("数据非法吗?");//手动生成异常//编译时就有异常//RuntimeException运行时的异常
//调用Exception的重载的构造器来写入message的信息
//类型是 String
}
}
}