java 异常捕捉(try catch finally)

一些关于try catch 简单总结。

return:
    在 trycatch语句块中:
        return 如果执行,需要先执行 finally 语句块(如果有),在返回值(方法值)
    在 finally 语句块中
        return 如果执行,将结束这个方法,直接返回值(方法值)(不会再跳回到 trycatch 语句块中)。
System.exit()
    如果执行,将会结束和终止整个程序(注意是整个程序)。
相关面试题
1. 下面程序的输出是什么?(B)
    package algorithms.com.guan.javajicu;
    public class TestDemo
    {
        public static String output = ””;
        public static void foo(inti)
        {
            try
            {
                if (i == 1)
                {
                    throw new Exception();
                }
            }
            catch (Exception e)
            {
                output += “2”;
                return ;
            } finally
            {
                output += “3”;
            }
            output += “4”;
        }
        public static void main(String[] args)
        {
            foo(0);
            foo(1);
            System.out.println(output);
        }
    }
    A. 342
    B. 3423
    C. 34234
    D. 323
    解析:
        这道题考察了 returncatch语句块中的情况。
        return 如果执行,需要先执行 finally 语句块(如果有),再返回值(方法值)
        return 执行完即结束。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值