5/17学习

22 篇文章 0 订阅
9 篇文章 0 订阅

Java处理异常(1)try…catch

try…catch语句的格式如下:

try {
    //包含可能发生异常的语句
}
catch(ExceptionSubClass1 e) {
    …
}
catch(ExceptionSubClass2 e) {
    …
}

Java处理异常(2)try…catch…finally

try…catch…finally语句的格式如下:
try {
    //包含可能发生异常的语句
}
catch(异常类名 异常对象) {
    //异常处理的代码
}
finally {
    //一定执行的代码
}

Java处理异常(3)throws

//throws处理异常的格式:

[访问权限修饰符] 返回值类型 方法名(参数列表) [throws 异常类名]{
    方法体;
    [return 返回值];
}
//throw异常处理方法:

//格式:
throw 异常对象;

Java自定义异常类

//自定义异常类的格式:

public class 异常类名 extends Exception {
    无参构造
    带参构造
}




public class DotcppException extends Exception {
    //无参构造
    public DotcppException() {
    } //带参构造
    public DotcppException(String message) {
        //异常错误消息
        super(message);
    }
}

Java try…catch…finally语句里return语句的执行顺序(1)

public class Main {
    public static void main(String[] args) {
        System.out.println(test1());
    }
    public static int test1() {
        int i =10;
        try {
            System.out.println("try语句");
            return --i;
        }
        catch (Exception e) {
            System.out.println("catch语句");
        }
        finally {
            System.out.println("finally语句");
        }
    return 0;
    }
}

Java try…catch…finally语句里return语句的执行顺序(2)

public class Main {
    public static void main(String[] args) {
        System.out.println(test2());
    }
    public static int test2() {
        int i =10;
        try {
            System.out.println("try语句");
            return --i;
        }
        catch (Exception e) {
            System.out.println("catch语句");
        }
        finally {
            System.out.println("finally语句");
            return --i;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏生十一_Nojambot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值