Raghav Chopra
Raghav Chopra

Reputation: 527

Crashing of apllication while using uncaught exception handler results to halt of application

I am using uncaught exception handler to catch exceptions but it results in the halt of my application. I am refering to this post How do I stop my application from zombifying after I handle an uncaught Excepition? as well as this Ideal way to set global uncaught exception Handler in Android

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.advsearch);
        setTitle("Advance Search");


    Thread.setDefaultUncaughtExceptionHandler(new SRSDexception(this));


    String trace = null;
    String line;
    try {
        BufferedReader reader = new BufferedReader(
        new InputStreamReader(SRSDAdvSearch.this
        .openFileInput("stack.trace")));

        while((line = reader.readLine()) != null) {
        trace += line+"\n";
        }
        } catch(FileNotFoundException fnfe) {
        // ...
        } catch(IOException ioe) {
        // ...
        }

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        String subject = "Error report";
        String body =
        "Mail this to [email protected]: "+
        "\n\n"+
        trace+
        "\n\n";

        sendIntent.putExtra(Intent.EXTRA_EMAIL,
        new String[] {"[email protected]"});
        sendIntent.putExtra(Intent.EXTRA_TEXT, body);
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        sendIntent.setType("message/rfc822");

        SRSDAdvSearch.this.startActivity(
        Intent.createChooser(sendIntent, "Title:"));

        SRSDAdvSearch.this.deleteFile("stack.trace");

This is my activity class which in turn calls my exception class After reading the url you will get to know what I want .. I have copied for uncaught exception which is

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) 
{   
//  System.out.println("You crashed thread " + t.getName());
//    System.out.println("Exception was: " + e.toString());
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 getCause
report += "--------- 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);
}

StackTrace

[android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314),   
SRTekBox.Android.SRSD.SRSDAdvSearch.fnExpand(SRSDAdvSearch.java:390),  
SRTekBox.Android.SRSD.SRSDAdvSearch$5.onClick(SRSDAdvSearch.java:209), 
android.view.View.performClick(View.java:2408), android.view.View$PerformClick.run (View.java:8816),
android.os.Handler.handleCallback(Handler.java:587),
android.os.Handler.dispatchMessage(Handler.java:92),
android.os.Looper.loop(Looper.java:123),
android.app.ActivityThread.main(ActivityThread.java:4633),
java.lang.reflect.Method.invokeNative(Native Method),
java.lang.reflect.Method.invoke(Method.java:521),
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858),
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616),
dalvik.system.NativeStart.main(Native Method)]

I don't know what the problem is, it can be

  1. my thread is getting crashed
  2. my handler is not invoked
  3. or its not taking exception to front but it results in halt of my application
  4. Exception makes it crash following possibilities

Can anybody help me out to prevent this halt

Upvotes: 2

Views: 3139

Answers (1)

Philippe Girolami
Philippe Girolami

Reputation: 1876

Use ACRA (http://code.google.com/p/acra/) ! They do what you need and have solved the issues you're going through

Upvotes: 3

Related Questions