Catch FileNotFoundException in AsyncTask method
Posted
by
Musterknabe
on Stack Overflow
See other posts from Stack Overflow
or by Musterknabe
Published on 2013-10-21T09:50:40Z
Indexed on
2013/10/21
9:53 UTC
Read the original article
Hit count: 277
I'm getting the favicon of a website with a method. Of course not every website has a favicon. So I want to catch it. The app doesn't crash if the website doesn't have a favicon but I still get an FileNotFoundException in the LogCat.
The problem I'm encountering is that I can't catch it
When I add `catch (FileNotFoundException f)
to my try-catch block it tells me
Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body.
The options I have is to remove it or to add a throws declaration to the doInBackground-method. The latter is not possible. This is the whole Try-Catch
try{
String baseURL = getBaseURL ( sourceURLArr[i] );
System.out.println(baseURL + "/favicon.ico");
Bitmap favicon = getBitmapFromURL( baseURL + "/favicon.ico");
Drawable fv = new BitmapDrawable(getResources(),
Bitmap.createScaledBitmap(favicon, 20, 20, true));
source [i].setCompoundDrawablesWithIntrinsicBounds(fv, null, null, null);
} catch(NullPointerException e){
} catch(FileNotFoundException f){
}
I already tried to switch the FileNotFoundException with the NullPointerException but it was the same error. When I add the throws to the asynctask do in background method I get
Exception FileNotFoundException is not compatible with throws clause in AsyncTask<Void,Void,Void>.doInBackground(Void[])
How can I now catch the FileNotFoundException?
© Stack Overflow or respective owner