如何处理try catch异常android [英] How to handle try catch exception android

查看:239
本文介绍了如何处理try catch异常android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用getBitmap方法来显示图像。当我使用它作为一种方法,如果它返回位图显示一个图像,但如果它返回null,捕获异常。但是如果输入的url也是错误的,它应该处理FileNotFoundException。如何处理两个异常并在UI中显示?

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


推荐答案

我真的, t推荐这个...

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

您是否正在查看堆栈跟踪以调试问题?跟踪它们不应该很难。看看LogCat并查看大块的红色文本,看看哪种方法导致你的崩溃和你的错误。

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.

如果你以这种方式捕获所有的错误,你的程序不会像预期的那样行事,当用户报告时,您不会收到Android Market的错误报告。

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.

您可以使用UncaughtExceptionHandler来防止某些崩溃。我使用一个,但只打印堆栈跟踪到一个文件,当我正在调试一个手机上的应用程序远离我的电脑。但是,在完成之后,我将默认的Android UncaughtExceptionHandler传递给未知的异常,因为我希望Android能够正确处理它,并给予用户发送堆栈跟踪的机会。

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.

这篇关于如何处理try catch异常android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆