0

「catch」句は無視できますか? 私はこのコードを持っています。私がしたいのは、特定の文字列を含むすべての単語をスキャンし、それらを String res に保存することです。

しかし、私が今持っているコードは、「catch」句が中断するとループが停止するため、ループを反復処理しません。catch 句を無視して、ファイルの最後に到達するまで「try」をループさせ続ける方法はありますか?

String delimiter = " - ";
String[] del;
String res = new String();

if(curEnhancedStem.startsWith("a"))
{
InputStream inputStream = getResources().openRawResource(R.raw.definitiona); 
try {
      BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
      String s = in.readLine();
      while(s != null)
        {
             s = in.readLine();
             del = s.split(delimiter);
             if (del[0].contains(curEnhancedStem))
             {
                 res = res + s + "\n\n";
             }
        }
          return res;
    }
    catch (Exception e) {
                // nothing to do here
            }   
        }
4

3 に答える 3

3

エラーが発生した後でも内側のループを続行したい場合は、別の try ブロックをそこに置くことができます。

String delimiter = " - ";
String[] del;
String res = new String();

if(curEnhancedStem.startsWith("a"))
{
InputStream inputStream = getResources().openRawResource(R.raw.definitiona); 
try {
  BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
  String s = in.readLine();
  while(s != null)
    {
         try {
             s = in.readLine();
             del = s.split(delimiter);
             if (del[0].contains(curEnhancedStem))
             {
                 res = res + s + "\n\n";
             }
         }
         catch (Exception e) {
            // Error in string processing code (as opposed to IO) - Don't care... Continue
        }   
    }


    }
    return res;
}
catch (Exception e) {
        // nothing to do here
        }   
    }

別のアイデアは、より具体的な例外を使用することです-一般的なすべての例外をキャッチするだけではありません

于 2013-03-04T13:06:38.343 に答える
2

内部で例外が発生しているに違いないので、これを試してください。

  try {
  BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
  String s = in.readLine();
  while(s != null)
    {
     try{
         s = in.readLine();
         del = s.split(delimiter);
         if (del[0].contains(curEnhancedStem))
         {
             res = res + s + "\n\n";
         }
        } catch(Exception e){
            // Do Something
      }
    }
      return res;
}
catch (Exception e) {
            // nothing to do here
        }   
    }

例外が発生した場合、ループ内で処理されますが、ループは続行されます。

于 2013-03-04T13:02:36.987 に答える
1

あなたのcatch句には何もありません。以下のようなものを for loop に追加して(ブロックwhileに入れておく)、どちらが得られたかを確認してください:tryexception

catch(Exception e)
{
   Log.e("Exception here: "+ e.getMessage());
}
于 2013-03-04T13:04:06.193 に答える