如何处理意外的错误和未捕获的异常 [英] how to handle unexpected error and uncaught exceptions

查看:135
本文介绍了如何处理意外的错误和未捕获的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用尝试捕获来处理一些预期的错误,
但有时,会有意想不到的错误。

I use "try catch" to handle some expected error, but sometime, there will be unexpected error.

我的意思是,如果我不使用试捕,如何处理崩溃的错误,并发送错误信息到服务器?

i mean if i don't use "try catch" , how to handle crash error ,and send error message to server?

我试试的UncaughtExceptionHandler
但它会阻止应用程序。

I have try "UncaughtExceptionHandler" , but it will block app.

感谢提前

推荐答案

一个尝试捕捉也将能够赶上意外的错误。你只要把他们在适当的顺序:最具体的,再比较一般。一个例子(这是一般的Java,没有特定的Andr​​oid code,但你会得到点):

A try catch will also be able to catch unexpected errors. You just have to put them in the appropriate order: the most specific first, then more general. An example (it's general Java, no specific Android code, but you'll get the point):

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

这篇关于如何处理意外的错误和未捕获的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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