标签:style io ar sp java on div 代码 bs
1.throw的作用
class Usre {
private int age;
public void setAge (int age) {
if(age < 0) {
RuntimeException e = new RuntimeException ("年龄不能为负数");//生成异常对象
throw e;//抛出
}
this.age = age;
}
}
class Test {
public static void main (String args []) {
User user = new User ();
user.setAge (-20);
}
}
throw的作用就是抛出异常对象
2.throws的作用
class Usre {
private int age;
public void setAge (int age) throws Exception {
if(age < 0) {
Exception e = new Exception ("年龄不能为负数");//生成异常对象
throw e;//抛出
}
this.age = age;
}
}
class Test {
public static void main (String args []) {
User user = new User ();
try{
user.setAge (-20);
}
catch {
System.out.println(e);
}
}
}
标签:style io ar sp java on div 代码 bs
原文地址:http://blog.csdn.net/u011742151/article/details/41282635