Android-忽略Catch子句?

Android-忽略Catch子句?,android,try-catch,ignore,notepad,Android,Try Catch,Ignore,Notepad,“catch”子句可以忽略吗?我有这段代码,我想做的是扫描包含特定字符串的所有单词,并将它们存储在string res中 但是我现在的代码没有遍历循环,因为当“catch”子句中断时,循环停止。有没有办法忽略catch子句,让“try”继续循环,直到到达文件末尾 String delimiter = " - "; String[] del; String res = new String(); if(curEnhancedStem.startsWith("a")) { InputStream

“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
            }   
        }

我想你一定是在里面遇到了异常,所以试试这个

  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
        }   
    }

如果您得到异常,它将在循环内处理,但您的循环将继续。

我认为您必须在循环内得到异常,所以请尝试此方法

  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
        }   
    }

如果您得到异常,它将在循环内处理,但您的循环将继续。

您的
catch
子句中没有任何内容。尝试为
while
循环添加如下内容(将其保留在
Try
块中),并找出您得到的
异常:

catch(Exception e)
{
   Log.e("Exception here: "+ e.getMessage());
}

您的
catch
子句中没有任何内容。尝试为
while
循环添加如下内容(将其保留在
Try
块中),并找出您得到的
异常:

catch(Exception e)
{
   Log.e("Exception here: "+ e.getMessage());
}

如果您真的希望它在内部循环中继续运行,即使在发生错误之后,您也可以在其中放置另一个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
        }   
    }

另一个想法是使用更具体的异常,而不仅仅是一般的“捕获所有异常”

,如果您确实希望它在内部循环中继续运行,即使在发生错误之后,您也可以在其中放置另一个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
        }   
    }


另一个想法是使用更具体的异常,而不仅仅是一般的catch-all异常

,它转到catch子句,因为代码中存在异常。将其发送回try子句不太可能对您有所帮助,因为在大多数情况下,您只会再次遇到异常。请尝试调试代码并修复异常。如果不在代码中编写,则可以忽略Yes catch子句。如果我不编写“catch”子句,我会收到一个错误,指出“readline()”应该用catch子句包围,或者我应该将Finally放在Try下面。它转到catch子句,因为代码中存在异常。将其发送回try子句不太可能对您有所帮助,因为在大多数情况下,您只会再次遇到异常。请尝试调试代码并修复异常。可以忽略Yes catch子句,方法是不在代码中编写它。如果我不编写“catch”子句,我会收到一个错误,指出“readline()”应该被catch子句包围,或者我应该将Finally放在Try下面。如果del[0]不包含CurenhancedItem,则会发生异常@jonellatienza然后尝试为while循环添加try/catch块,并在DDMS中获取消息并修复问题..编码快乐!!!无法编辑,因为字符计数应该是,而不是+出现异常“如果del[0]不包含currenhancedstem”。@jonelletienza然后尝试为while循环添加try/catch块,并在DDMS中获取消息并修复问题。.编码愉快!!!无法编辑,因为字符计数应该是,而不是+哦,我试试这个!这可能就是我需要的!谢谢你好非常感谢你的想法。代码现在可以正常工作了!:)哦,我试试这个!这可能就是我需要的!谢谢你好非常感谢你的想法。代码现在可以正常工作了!:)外部的try-catch块可能不必要我明白了。。。不知道可以放置多个try/catch。。。我试试这个!非常感谢。你好代码有效!谢谢!我会选择这个作为最佳答案,但AnujMathur07在4分钟前发布。我感谢你的帮助!外部的try-catch块可能不必要我明白了。。。不知道可以放置多个try/catch。。。我试试这个!非常感谢。你好代码有效!谢谢!我会选择这个作为最佳答案,但AnujMathur07在4分钟前发布。我感谢你的帮助!