Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle try catch exception android

I am using a method getBitmap to display images. As I am using this as a method,if it returns bitmap display an image but if it returns null,catch an exception. But if url entered is wrong also, it should handle the FileNotFoundException. How to handle two exception and display in UI?

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 Activity, i've given 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.
        }          
like image 276
Shadow Avatar asked Mar 07 '14 11:03

Shadow


People also ask

How do I handle exceptions in Android?

How Exceptions Work in JVM and Android. In Java, all exception and error types are subclasses of Throwable. The Throwable class changes the execution flow of JVM apps by throwing exceptions and deciding how to recover from an error. Adding an exception to a code changes its execution flow.

How do I get rid of try catch?

It's not required and we can delete specific Catch block. Just right click on unwanted Catch block and click Delete to delete it.

What is try catch in Android?

Kotlin try-catch block is used for exception handling in the code. The try block encloses the code which may throw an exception and the catch block is used to handle the exception. This block must be written within the method.

How do I stop multiple try catch blocks?

To avoid writing multiple try catch async await in a function, a better option is to create a function to wrap each try catch. The first result of the promise returns an array where the first element is the data and the second element is an error. And if there's an error, then the data is null and the error is defined.


2 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 your stack traces to debug your issues? It should not be hard to track them down. Look at LogCat and review the big block of red text to see which method caused your crash and what your error was.

If you catch all your errors this way, your program is not going to behave as expected, and you will not get error reports from Android Market when your users report them.

You can use an UncaughtExceptionHandler to possibly prevent some crashes. I use one, but only to print stack traces to a file, for when I'm debugging an app on a phone away from my computer. But I pass on the uncaught exception to the default Android UncaughtExceptionHandler after I've done that, because I want Android to be able to handle it correctly, and give the user the opportunity to send me a stack trace.

like image 147
Jebasuthan Avatar answered Sep 19 '22 14:09

Jebasuthan


// try is nothing but a way to communicate a program 
  Example:

  try{ 
        //code here File IO operation like Files 
        // Code here Network Operation  
        //code here /0 (Divide by Zero )


     }catch (Exception e) {
        Log.e("Fail 2", e.toString());
         //At the level Exception Class handle the error in Exception Table 
         // Exception Create That Error  Object and throw it  
         //E.g: FileNotFoundException ,etc
        e.printStackTrace();
    }finally {
        //it always execute
    }
like image 36
Ashif Avatar answered Sep 23 '22 14:09

Ashif