Catch All Type Exceptions Programming Android
Solution 1:
I really, really don't recommend this...
try {
...
} catch (Exception e) {
// This will catch any exception, because they are all descended from Exception
}
Are you looking at your stack traces to debug your issues? It should not be hard to track them down. Look at LogCat and review the big block of red text to see which method caused your crash and what your error was.
If you catch all your errors this way, your program is not going to behave as expected, and you will not get error reports from Android Market when your users report them.
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.
Solution 2:
I'm assuming like pure java
try {
} catch(throwable t) {
}
But this is Very Bad Practice.
Also look at
Solution 3:
If you're on Eclipse, every exception that Force-Closes the app (aka the message you mention) should be logged in the "LogCat".
The easiest way to see the LogCat, is to Open the DDMS perspective and clic on the LogCat tab (or open it from the "View" menu if it's not already displayed).
Solution 4:
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
publicclassSRSDexceptionimplementsThread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler defaultUEH;
privateActivityapp=null;
publicSRSDexception(Activity app) {
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
this.app = app;
}
publicvoiduncaughtException(Thread t, Throwable e)
{
StackTraceElement[] arr = e.getStackTrace();
StringRaghav=t.toString();
Stringreport= 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 getCause
report += "--------- Cause ---------\n\n";
Throwablecause= 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 {
FileOutputStreamtrace= app.openFileOutput(
"stack.trace", Context.MODE_PRIVATE);
trace.write(report.getBytes());
trace.close();
} catch(IOException ioe) {
// ...
}
defaultUEH.uncaughtException(t, e);
}
}
Post a Comment for "Catch All Type Exceptions Programming Android"