【问题标题】:How to prevent a runtime error to stop the rest of the code in try/catch from executing?如何防止运行时错误阻止 try/catch 中的其余代码执行?
【发布时间】:2014-07-23 16:36:05
【问题描述】:

我承认主标题相当混乱。让我解释一下。

我在 try 块内有一个 while 循环,然后是 catch。 while 循环任务是从 rss 提要生成的链接中检索大量图片。有时链接中没有图片,所以它给了我 FileNotFoundException,但大多数时候它没有。问题是当出现错误时,while 循环会停止。出现错误如何防止它停止?

例子:

try
{
    ...

    while
    {
        ...
        Bitmap thumbnail = getBitmapFromUrl(URL);
        ...
    }
}
(a bunch of catch blocks here)

这里是getBitmapFromUrl函数:

public static InputStream getInputStream(URL url)
{
    try
    {
        return url.openConnection().getInputStream();
    }
    catch (IOException e)
    {
        return null;
    }
}

public static Bitmap getBitmapFromURL(String src)
{
    try
    {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    }
    catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }
}

请注意,这两个函数中还有 try/catch 块。所以基本上在 try/catch 中的 try/catch 中有 try/catch。当有 FileNotFoundException 时(我假设 IOException 也会捕获这个异常),会执行哪个 catch?

还有一个主要问题,即使发现错误,如何保持 while 循环运行?如果在 URL 中发现错误,则 Bitmap 为 null,如上所示(在 IOException catch 块中返回 null)。

这是来自 LogCat 的错误:

06-03 17:27:06.444: W/System.err(10831): java.io.FileNotFoundException: https://s3.amazonaws.com/uploads.hipchat.com/61622/424982/hd4fnFSi1IAwxV6/acere700_thumbnail.jpg
06-03 17:27:06.454: W/System.err(10831):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
06-03 17:27:06.454: W/System.err(10831):    at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
06-03 17:27:06.454: W/System.err(10831):    at com.anggrian.readee.Global.getBitmapFromURL(Global.java:61)
06-03 17:27:06.454: W/System.err(10831):    at com.anggrian.readee.MainActivityFragment2$mAsyncTask.doInBackground(MainActivityFragment2.java:230)
06-03 17:27:06.454: W/System.err(10831):    at com.anggrian.readee.MainActivityFragment2$mAsyncTask.doInBackground(MainActivityFragment2.java:1)
06-03 17:27:06.454: W/System.err(10831):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-03 17:27:06.454: W/System.err(10831):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-03 17:27:06.464: W/System.err(10831):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-03 17:27:06.464: W/System.err(10831):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-03 17:27:06.474: W/System.err(10831):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-03 17:27:06.474: W/System.err(10831):    at java.lang.Thread.run(Thread.java:841)

感谢您的帮助! (如果有的话)

【问题讨论】:

    标签: android exception try-catch


    【解决方案1】:

    try/catch 块放在 while 循环中。如果捕获到异常,将执行 catch 块,然后循环使用下一个元素继续:

    while(true){
        try {
            //...
            Bitmap thumbnail = getBitmapFromUrl(URL);
            //...
        } catch (Exception e){
            e.printStackTrace()
        }
        //resumes here (inside the loop) afer an Exception was caught
    }
    

    【讨论】:

    • 循环上方的某些代码要求它们位于 try/catch 中。但是如果我把try/catch放在循环里面,而循环也在try/catch里面,会执行哪个异常呢?
    • 异常会在最里面的catch块中被捕获,所以它会在while循环中。
    【解决方案2】:

    你的 try-catch 块应该在循环内。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 2012-11-09
      • 1970-01-01
      • 2015-11-01
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多