2014-05-20 67 views
0

我用“try catch”来处理一些预期的错误,但有时候会出现意想不到的错误。如何处理意外错误和未捕获的异常

我的意思是如果我不使用“try catch”,如何处理崩溃错误,并发送错误消息到服务器?

我已尝试“UncaughtExceptionHandler”, 但它会阻止应用程序。

在此先感谢

+0

你可以catch(Excepction前) – PearsonArtPhoto

回答

0

尝试捕获也将能够捕获意外的错误。你只需要按照适当的顺序放置它们:最具体的第一个,然后是更一般的。一个例子(这是一般的Java,没有具体的Android代码,但你会得到的):

try { 
     int input = sc.nextInt(); //random number 
     int amount = 10/input; // not every result can be stored in an int 
    } catch (InputMismatchException ime) { 
     // catches error of non-numeric value 
     System.out.println("input is not a valid number"); 
    } catch (ArithmeticException ae) { 
     //catches error of division by 0 
     System.out.println("Division by 0!"); 
    } 
    catch(Exception ex) 
    { 
     // catches any other error 
     System.out.println("other error"); 
    }