How to handle try catch catch android

I use the getBitmap method to display images. Since I use this as a method, if it returns a bitmap, an image, but if it returns null, catch the exception. But if an invalid url is entered, it should handle a FileNotFoundException. How to handle two exceptions and display in the user interface?

public Bitmap getBitmap(final String src) { try { InputStream stream = null; URL url = new URL(src); java.net.URL url = new java.net.URL(src); URLConnection connection = url.openConnection(); InputStream input = connection.getInputStream(); myBitmaps = BitmapFactory.decodeStream(input); return myBitmaps; } catch (IOException e) { e.printStackTrace(); Log.e("IO","IO"+e); return null; } catch(OutOfMemoryError e1) { e1.printStackTrace(); Log.e("Memory exceptions","exceptions"+e1); return null; } } 

In action, I gave like this

  Bitmap filename=service.getBitmap(url_box.getText().toString()); if(file_name!=null) { displaybitmap(file_name); } else { //Toast.makeText("Memory Error"); //my question is how to handle two exception in UI where memory error and also // when entered url is wrong, file not found exceptions also to handle. } 
+6
source share
4 answers

I really, really don't recommend this ...

 try { ... } catch (Exception e) { // This will catch any exception, because they are all descended from Exception System.out.println("Error " + e.getMessage()); return null; } 

Are you looking at stack traces to debug your problems? They should not be difficult to track. Look at LogCat and look at the large block of red text to find out which method caused your failure and what your error was.

If you catch all your errors in this way, your program will not behave as expected, and you will not receive error reports from the Android Market when your users report them.

You can use UncaughtExceptionHandler to prevent some crashes. I use one, but only to print stack traces to a file, because when I debug the application on the phone from my computer. But after I did this, I move on to the unclaimed exception from the standard Android UncaughtExceptionHandler, because I want Android to be able to handle it correctly and give the user the ability to send me a stack trace.

+4
source

Check catch expressions

 catch (IOException e) { e.printStackTrace(); Log.e("IO","IO"+e); return null; } catch(OutOfMemoryError e1) { e1.printStackTrace(); Log.e("Memory exceptions","exceptions"+e1); return null; } 

Here you return null in both exceptions, My sentence initializes the variable in these catch clauses , and in your activity method checks the value of this variable.

Like this

  String exceptionName=""; catch (IOException e) { e.printStackTrace(); Log.e("IO","IO"+e); exceptionName="IOException"; return null; } catch(OutOfMemoryError e1) { e1.printStackTrace(); Log.e("Memory exceptions","exceptions"+e1); exceptionName="OutOfMemoryError"; return null; } 

Now in your activity

  Bitmap filename=service.getBitmap(url_box.getText().toString()); if(file_name!=null) { displaybitmap(file_name); } else { //Toast.makeText("Memory Error"); //my question is how to handle two exception in UI where memory error and also // when entered url is wrong, file not found exceptions also to handle. if (exceptionName.equals("OutOfMemoryError")) { // Handle here } else{ // Handle here } } 
+2
source

Return the default bitmap from your res. But there is a very good Universal Image Loader bitmap library, check it out.

+1
source

Instead, exclude the exceptions from the getBitmap method and let the client (Act) handle the exceptions, in this case either you will get a bitmap or an exception and you can skip the return null bitmap and execute the corresponding "default load bitmap" into catch blocks, instead of the case exceptions / errors (since this is an empty case now).

 public Bitmap getBitmap(final String src) throws FileNotFoundException, OutOfMemoryError, IOException, MalformedURLException { URL url = new URL(src); URLConnection connection = url.openConnection(); InputStream input = connection.getInputStream(); return BitmapFactory.decodeStream(input); } 
0
source

All Articles