mardi 3 juin 2014

How to prevent a runtime error to stop the rest of the code in try/catch from executing?


Vote count:

0




I admit the main title is quite confusing. Let me explain it.


I have a while loop inside a try block followed by catch. The while loop task is to retrieve a lot of pictures from links generated by rss feeds. Sometimes there is no picture in the link so it gave me FileNotFoundException, but most of the time it doesn't. The problem is when there is an error, the while loop stops. How to prevent it from stopping even if there is an error?


Example:



try
{
...

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


here is getBitmapFromUrl function:



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


notice there is also try/catch blocks in both of the functions. So basically there is try/catch within a try/catch in a try/catch. When there is FileNotFoundException (I assume IOException will also catch this exception), which catch will be executed?


And the main question, how to keep the while loop running even if it found an error? If an error is found within an URL, the Bitmap is null as code shown above (return null in IOException catch block).


Here's an error from LogCat:



06-03 17:27:06.444: W/System.err(10831): java.io.FileNotFoundException: http://ift.tt/1ht87Bf
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)


Thanks for the help! (if any)



asked 16 secs ago






Aucun commentaire:

Enregistrer un commentaire