-1
while (true) {
    try {
        //create ImportantObject
        LoopToGoTo:
        for (int i = 0; i < Limit; i++) {
            //method in here throws the exception
            //ImportantObject is used in here.
        }
    } catch(Exception e) {
        //report error
        continue LoopToGoTo;
    }
}

trycatchブロック内のループを続行したいと思います。これは可能ですか?

編集:申し訳ありませんが、trycatchをforループ内に移動できなかった理由がわかりませんでした。(編集されたスニペット)forループ内にtry-catchを配置すると、内部の呼び出しは、ImportantObjectにアクセスできなくなります。これは私が立ち往生している場所です。

EDIT2:さて、ラベルを続行せずに、問題を解決しました!私の質問に対する答えは単純な「いいえ」だと思います。悪い習慣はいたるところにあるかもしれませんが、私の割り当ては2時間で期限が切れます。私は何を言うことができます:D

//ImportantClass ImportantObject = null;
while (!ranOnce) {
    try {
        //create ImportantObject
        ranOnce = true;
    } catch(Exception e) {
        //report error
        continue;
    }
}
for (int i = 0; i < Limit; i++) {
    try {
        //method in here throws the exception
        //ImportantObject is used in here.
    } catch(Exception e) {
        //report error
        continue;
    }
}
4

3 に答える 3

2

いいえ、それはできません。既に try ブロックの範囲外にいるためです。

なぜ try キャッチを for ループ内に移動しないのでしょうか?

for (int i = 0; i < Limit; i++) {
    try {
        //method in here throws the exception
    } 
    catch(Exception e) {
    //report error
    }
}
于 2012-09-13T20:38:00.547 に答える
1

これをやりたいということですか?

        for (int i = 0; i < Limit; i++) {
          try {
            //method in here throws the exception
          } catch(Exception e) {
            //report error
          }
        }
于 2012-09-13T20:38:50.813 に答える
0

continue;あなたが達成したいことは、単純なものでも同じ効果があると思います。

于 2012-09-13T20:39:02.137 に答える