标签:
classTest{
publicstaticvoid main(String args[]){
System.out.println(1);
//uncheck exception
int i =1/0;
System.out.println(2);
}
}
classTestCheck{
publicstaticvoid main(String args []){
//check exception
Thread.sleep(1000);
}
}
classTest{
publicstaticvoid main(String args[]){
System.out.println(1);
//uncheck exception
try{
//打开文件
System.out.println(2);
int i =1/0;
System.out.println(3);
}
catch(Exception e){
e.printStackTrace();
System.out.println(4);
}
finally{
System.out.println("finally");
//关闭文件
}
System.out.println(5);
}
}
classUser{
privateint age;
publicvoid setAge(int age){
if(age <0){
RuntimeException e =newRuntimeException();
throw e;
}
this.age = age;
}
}
classTest{
publicstaticvoid main(String args []){
User user =newUser();
user.setAge(-20);
}
}
classUser{
privateint age;
publicvoid setAge(int age) throws Exception{
if(age <0){
RuntimeException e =newRuntimeException("年龄不能为负数");
throw e;
}
this.age = age;
}
}
classTest{
publicstaticvoid main(String args []){
User user =newUser();
try{
user.setAge(-20);
}
catch(Exception e){
System.out.println(e);
}
}
}
标签:
原文地址:http://www.cnblogs.com/arroneve/p/5815459.html