发布时间:2019-08-30 08:31:09编辑:auto阅读(1929)
声明异常:
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环境的搭
50051
49306
39969
37010
31404
28245
27201
21993
21950
20278
408°
303°
286°
465°
450°
1064°
1995°
1850°
1469°
2975°