发布时间:2019-08-30 08:31:09编辑:auto阅读(2749)
声明异常:
throws,不处理异常,丢给调用者处理
public static void f() throws IOException{
...
}
丢给调用方法处理
public static void main(String[]args) throws IOException{
f();
}
main()方法丢给jre处理
自定义异常:
在运行时定义的异常用throw,如果是编译器异常则需要throws或try-catch,比如继承的是Exception
Person p=new Person();
p.setAge(-1);
class Person{
private int age;
public void setAge(int age)
{
if(age<0)
{
throw new ill("年龄不能为负数");
}
}
public int getAge()
{
return age;
}
}
class ill extends RuntimeException{ //继承RuntimeException
public ill()
{
}
public ill(String s)
{
super(s); //使用父类的构造类方法
}结果为:年龄不能为负数
上一篇: linux下的sqlite3的编译安装和
下一篇: windows系统下Python环境的搭
55116
41918
36501
31906
26813
26482
25020
20643
16552
15965
3179°
2875°
2969°
3120°
3052°
3327°
3225°
3056°
3049°
3027°