休息只会留下try / catch或周围的循环吗? [英] Does a break leave just the try/catch or the surrounding loop?

查看:101
本文介绍了休息只会留下try / catch或周围的循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个尝试...抓住阻止循环,并且#sa catch 中打破,程序执行是否会离开循环?

If I have a try ... catch block inside a while loop, and there#s a break inside the catch, does program execution leave the loop?

如:

while (!finished) {
    try {
        doStuff();
    } catch (Exception e) {
        break;
    }
}

doStuff()中抛出的异常是否会退出循环?

Will an exception thrown in doStuff() exit the loop?

推荐答案

是的,它会。最简单的方法就是试一试。

Yes, it will. Easiest way to find out is to try it.

public static void main(String[] args) {
        int i=0;
        while (i<10) {
            System.out.println(i);
            try {
                if(i ==7){
                    throw new Exception();
                }
                i++;
            } catch (Exception e) {
                break;
            }
        }
        System.out.println("out of loop");
    }

它将打印

0
1
2
3
4
5
6
7
out of loop

输出从0开始。

这篇关于休息只会留下try / catch或周围的循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆