【问题标题】:Android - Ignoring Catch Clause?Android - 忽略 Catch 条款?
【发布时间】:2013-03-04 12:59:30
【问题描述】:

可以忽略“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
            }   
        }

【问题讨论】:

  • 它转到 catch 子句,因为您的代码中有异常。将它发送回 try 子句不太可能对您有所帮助,因为在大多数情况下,您只会再次遇到异常。尝试调试您的代码并修复异常。
  • Yes catch 子句可以被忽略,不用写在代码里。

标签: android try-catch notepad


【解决方案1】:

如果您真的希望它在发生错误后继续在内循环中,您可以在其中放置另一个 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-catch 块可能是不必要的
  • 我明白了...不知道可以放置多个尝试/捕获...我试试这个!谢谢!
  • 嗨!代码有效!非常感谢!我会选择此作为最佳答案,但 AnujMathur_07 提前 4 分钟发布。感谢您的帮助!
【解决方案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
        }   
    }

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

【讨论】:

  • 哦,我试试这个!这可能是我需要的!谢谢!
  • 嗨!非常感谢你的想法。代码现在可以正常工作了! :)
【解决方案3】:

catch 子句中没有任何内容。尝试为while 循环添加类似下面的内容(将其保留在try 块中)以及找出你得到了哪个exception

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

【讨论】:

  • 我得到的异常发生在“如果 del[0] 不包含 curEnhancedStem”。
  • @JonelleAtienza 然后尝试为 while 循环添加 try/catch 块,并在 DDMS 中获取消息并修复问题..快乐编码!!!
  • 不能编辑,因为字符数应该是,而不是 + 那里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2018-07-23
  • 2011-02-07
  • 2011-10-19
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多