【问题标题】:How to handle try catch exception android如何处理尝试捕获异常android
【发布时间】:2014-04-10 11:53:14
【问题描述】:

我正在使用 getBitmap 方法来显示图像。当我使用它作为方法时,如果它返回位图显示图像但如果它返回 null,则捕获异常。但是如果输入的 url 也是错误的,它应该处理 FileNotFoundException。如何处理两个异常并在 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;
        }
        }

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

【问题讨论】:

  • 没有办法处理 NullPointer 异常。您必须避免空实例。

标签: android exception-handling bitmap out-of-memory filenotfoundexception


【解决方案1】:

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

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 并查看大块红色文本,了解是哪种方法导致了您的崩溃以及您的错误是什么。

如果您以这种方式捕获所有错误,您的程序将无法按预期运行,并且当您的用户报告错误时,您将不会从 Android Market 收到错误报告。

您可以使用 UncaughtExceptionHandler 来防止某些崩溃。我使用一个,但仅用于将堆栈跟踪打印到文件中,以便在远离计算机的手机上调试应用程序时使用。但是我在完成之后将未捕获的异常传递给默认的 Android UncaughtExceptionHandler,因为我希望 Android 能够正确处理它,并让用户有机会向我发送堆栈跟踪。

【讨论】:

  • 是的@Jeba 你是对的。我也在寻找 uncaughtExceptionHandler 但我不知道如何在服务和活动之间使用。由于此方法正在使用中,并且希望在活动中处理它。因为我的想法是不要使用try catch。但我不知道如何使用和处理。
【解决方案2】:

从您的资源中返回一个默认位图。但是有一个非常好的用于处理位图的库,称为 Universal Image Loader,请查看。

【讨论】:

    【解决方案3】:

    检查你的 catch 表达式

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

    在这里,您在两个异常中都返回 null,我的建议是在这些 catch 子句中初始化一个变量,并在您的活动方法中检查该变量的值。 p>

    像这样

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

    现在在你的活动中

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

    【讨论】:

    • k..会尝试让你知道@Brontok
    【解决方案4】:

    从getBitmap 方法中抛出异常,让客户端(Activity)处理异常,在这种情况下,您要么收到位图,要么收到异常,并且可以跳过return null 位图并执行相应的“默认位图”在 catch 块中加载“,而不是异常/错误情况(因为现在这是空情况)。

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

    【讨论】:

      【解决方案5】:
      // 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
          }
      

      【讨论】:

        猜你喜欢
        • 2018-02-06
        • 1970-01-01
        • 2015-04-14
        • 2013-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 2021-12-13
        相关资源
        最近更新 更多