Catch all type exceptions programming Android
来源:互联网 发布:金日十数据 编辑:程序博客网 时间:2024/04/30 10:45
转:http://stackoverflow.com/questions/5773993/catch-all-type-exceptions-programming-android
Q:
I'm developing an application for Android OS. Since this is my first application, I think I've committed some programming mistakes cause I hardly can trace bugs back to their causes. Thus, I was guessing, while i'm trying to fix bugs, is there a way to catch ALL types of exception in my entire activity lifecycle with one try-catch?
That would be awesome, i'm getting bored watching my galaxy S say :"Sorry the application App has stopped unexpectly" :(
A:
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.
Code:
import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.content.Context;public class SRSDexception implements Thread.UncaughtExceptionHandler {private Thread.UncaughtExceptionHandler defaultUEH;private Activity app = null;public SRSDexception(Activity app) {this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); this.app = app;}public void uncaughtException(Thread t, Throwable e) { StackTraceElement[] arr = e.getStackTrace();String Raghav =t.toString();String report = e.toString()+"\n\n";report += "--------- Stack trace ---------\n\n"+Raghav;for (int i=0; i<arr.length; i++){report += " "+arr[i].toString()+"\n";}report += "-------------------------------\n\n";// If the exception was thrown in a background thread inside// AsyncTask, then the actual exception can be found with getCausereport += "--------- Cause ---------\n\n";Throwable cause = e.getCause();if(cause != null) {report += cause.toString() + "\n\n";arr = cause.getStackTrace();for (int i=0; i<arr.length; i++){report += " "+arr[i].toString()+"\n";}}report += "-------------------------------\n\n";try {FileOutputStream trace = app.openFileOutput("stack.trace", Context.MODE_PRIVATE);trace.write(report.getBytes());trace.close();} catch(IOException ioe) {// ...}defaultUEH.uncaughtException(t, e);}}
- Catch all type exceptions programming Android
- Programming with Exceptions
- exceptions be caught by catch
- Exceptions不能跨进程catch
- Unknown media type in type 'all/all'
- codeforces757A. Gotta Catch Em' All!
- A. Gotta Catch Em' All!
- Gotta Catch Em' All!_Codeforces
- Type cannot use 'try' with exceptions disabled
- customize SysMailer class to catch exceptions in the send() method
- Unknown media type in type 'all/all' 问题解决方案
- Unknown media type in type ‘all/all’ 的解决办法
- Item 58: Used checked exceptions for recoverable conditions and runtime exceptions for programming errors
- R Programming week1-Data Type
- codeforces 757 A. Gotta Catch Em' All!
- Codeforces 757 Gotta Catch Em' All!
- 【codeforces 757A】Gotta Catch Em' All!
- Exceptions
- PE结构详解
- Redis数据类型
- 第六阶段:Activity和Intent的学习
- AEAI Miscdp文件上传功能使用心得
- Leetcode Single Number
- Catch all type exceptions programming Android
- 集社交与金融为一体,平安天下通如何推动互联网金融创新?
- java中volatile关键字的含义
- ios基础知识
- spring框架中的事务管理
- Redis 在新浪微博中的应用
- 使用heartbeat+monit实现主备双热备份系统
- NSString 值传不进问题,报optimized out错误
- Android源码目录结构