首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

用UncaughtExceptionHandler处理意外错误

2013-08-13 
用UncaughtExceptionHandler处理意外异常?UncaughtExceptionHandler接口用来捕获运行时未被程序捕获异常.?

用UncaughtExceptionHandler处理意外异常

?UncaughtExceptionHandler接口用来捕获运行时未被程序捕获异常.?

?

用处:

# 某些特殊情况下的异常处理以及线程重新load,从而成为”永远存在“的线程。

# 友好退出,善后处理,比如保存游戏币等

?

使用:

Java代码??用UncaughtExceptionHandler处理意外错误
  1. try?{??
  2. ????Thread?caughtableThread?=?new?Thread(new?Task());??
  3. ????caughtableThread.setUncaughtExceptionHandler(new?SimpleUncaughtExceptionHandler());??
  4. ????caughtableThread.start();??
  5. }?catch?(Exception?e)?{??
  6. ????System.out.print("can?do?somthing");//依然不会出执行??
  7. ????e.printStackTrace();??
  8. }??

?

Java代码??用UncaughtExceptionHandler处理意外错误
  1. static?class?SimpleUncaughtExceptionHandler?implements?UncaughtExceptionHandler?{??
  2. ??
  3. ????@Override??
  4. ????public?void?uncaughtException(Thread?t,?Throwable?e)?{??
  5. ????????System.err.println(t.getName()?+?"??"?+e.getMessage());//这里会捕获异常。??
  6. ????????e.printStackTrace();??
  7. ????}??
  8. } ?

?

Android中:

1、异常处理类,代码如下:

?

public class CrashHandler implements UncaughtExceptionHandler {    public static final String TAG = "CrashHandler";    private static CrashHandler INSTANCE = new CrashHandler();    private Context mContext;    private Thread.UncaughtExceptionHandler mDefaultHandler;    private CrashHandler() {    }    public static CrashHandler getInstance() {        return INSTANCE;    }    public void init(Context ctx) {        mContext = ctx;        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();        Thread.setDefaultUncaughtExceptionHandler(this);    }    @Override    public void uncaughtException(Thread thread, Throwable ex) {        // if (!handleException(ex) && mDefaultHandler != null) {        // mDefaultHandler.uncaughtException(thread, ex);        // } else {        // android.os.Process.killProcess(android.os.Process.myPid());        // System.exit(10);        // }        System.out.println("uncaughtException");        new Thread() {            @Override            public void run() {                Looper.prepare();                new AlertDialog.Builder(mContext).setTitle("提示").setCancelable(false)                        .setMessage("程序崩溃了...").setNeutralButton("我知道了", new OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                System.exit(0);                            }                        })                        .create().show();                Looper.loop();            }        }.start();    }    /**     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑     *      * @param ex     * @return true:如果处理了该异常信息;否则返回false     */    private boolean handleException(Throwable ex) {        if (ex == null) {            return true;        }        // new Handler(Looper.getMainLooper()).post(new Runnable() {        // @Override        // public void run() {        // new AlertDialog.Builder(mContext).setTitle("提示")        // .setMessage("程序崩溃了...").setNeutralButton("我知道了", null)        // .create().show();        // }        // });        return true;    }}

?

?

2、线程绑定异常处理类

public class CrashHandlerActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        CrashHandler crashHandler = CrashHandler.getInstance();          crashHandler.init(this);  //传入参数必须为Activity,否则AlertDialog将不显示。        // 创建错误        throw new NullPointerException();    }}

?

?

参考:

http://orgcent.com/android-uncaughtexceptionhandler-exception/

http://fantaxy025025.iteye.com/admin/blogs/new

?

?

热点排行