java 异常处理3

2020-12-13 06:09

阅读:478

标签:class   cep   pre   异常处理   年龄   except   exception   strong   编译   

声明异常:
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

java 异常处理3

标签:class   cep   pre   异常处理   年龄   except   exception   strong   编译   

原文地址:https://blog.51cto.com/14437184/2419087


评论


亲,登录后才可以留言!