Quantcast

Implementing 'UncaughtExceptionHandler'

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Implementing 'UncaughtExceptionHandler'

PedroC
Hello
I'm trying to implement 'UncaughtExceptionHandler' for Monodroid as seen in the following link:
 
http://stackoverflow.com/questions/601503/how-do-i-obtain-crash-data-from-my-android-application

but with no success.
I have tried to code as you see next but with no success and can't find anyware an implementation for 'IUncaughtExceptionHandler'.
 I hope someone can direct me in the right way to do it.


public interface CMUncaughtExceptionHandler : Java.Lang.Thread.IUncaughtExceptionHandler, Android.Runtime.IJavaObject, IDisposable
 {
        string outfile = "";

        public CMUncaughtExceptionHandler(string file)
        {
                outfile = file;
        }

        void UncaughtException(Java.Lang.Thread t, Java.Lang.Throwable e)
        {
            //Send 'e.printStackTrace' to outfile            

        }
}

Java.Lang.Thread.DefaultUncaughtExceptionHandler = new CMUncaughtExceptionHandler();
 

Thanks for any help
P.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Implementing 'UncaughtExceptionHandler'

Sayed Arian Kooshesh
jon, correct my if I'm wrong but that(DefaultUncaughtExceptionHandler) only catches java errors. for uncaught .net errors:

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

use lightly and make sure on different threads you reassign it. (afaik) it only tacks exceptions for the current thread but i'm not sure on that.

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Implementing 'UncaughtExceptionHandler'

Jonathan Pryor-2
In reply to this post by PedroC
On Jun 25, 2012, at 7:13 PM, PedroC wrote:
> Hello. I'm trying to implement 'UncaughtExceptionHandler' for Monodroid as seen in the following link:
>
> http://stackoverflow.com/questions/601503/how-do-i-obtain-crash-data-from-my-android-application
>
> but with no success. I have tried to code as you see next but with no success and can't find anyware an implementation for 'IUncaughtExceptionHandler'.

IUncaughtExceptionHandler is a nested type; it's Java.Lang.Thread.IUncaughtExceptionHandler:

        http://androidapi.xamarin.com/?link=T%3aJava.Lang.Thread.IUncaughtExceptionHandler

Furthermore, you should never implement IJavaObject yourself. Instead, you should inherit Java.Lang.Object:

        public class CMUncaughtExceptionHandler : Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler {
                public void UncaughtException(Thread thread, Throwable throwable)
                {
                        // ...
                }
        }

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Implementing 'UncaughtExceptionHandler'

PedroC
Sayed, Jon, thanks for your help.

to catch all the unhandled exceptions in an Android application what method is more complete, the Java or the .NET implementation ?
Does the Java method only catch Java errors ?

Thanks again
Pedro
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Implementing 'UncaughtExceptionHandler'

umashankar
This post has NOT been accepted by the mailing list yet.
In reply to this post by Jonathan Pryor-2
Hi Jon,

I implemented the Java.Lang.Thread.IUnCaughtExceptionHandler in c# class.

class ExceptionHandler : Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler
    {
        private Activity myContext;
        private string LINE_SEPARATOR = "\n";
        public ExceptionHandler(Activity context)
        {
            myContext = context;
        }
        public void UncaughtException(Java.Lang.Thread thread, Java.Lang.Throwable ex)
        {

            StringWriter stackTrace = new StringWriter();
            ex.PrintStackTrace(new PrintWriter(stackTrace));
            StringBuilder errorReport = new StringBuilder();
            errorReport.Append("************ CAUSE OF ERROR ************\n\n");
            errorReport.Append(stackTrace.ToString());
            errorReport.Append("\n************ DEVICE INFORMATION ***********\n");
            errorReport.Append("Brand: ");
            errorReport.Append(Build.Brand);
            errorReport.Append(LINE_SEPARATOR);
            errorReport.Append("Device: ");
            errorReport.Append(Build.Device);
            errorReport.Append(LINE_SEPARATOR);
            errorReport.Append("Model: ");
            errorReport.Append(Build.Model);
            errorReport.Append(LINE_SEPARATOR);
            errorReport.Append("Id: ");
            errorReport.Append(Build.Id);
            errorReport.Append(LINE_SEPARATOR);
            errorReport.Append("Product: ");
            errorReport.Append(Build.Product);
            errorReport.Append(LINE_SEPARATOR);
            errorReport.Append("\n************ FIRMWARE ************\n");
            errorReport.Append("SDK: ");
            errorReport.Append(Build.VERSION.Sdk);
            errorReport.Append(LINE_SEPARATOR);
            errorReport.Append("Release: ");
            errorReport.Append(Build.VERSION.Release);
            errorReport.Append(LINE_SEPARATOR);
            errorReport.Append("Incremental: ");
            errorReport.Append(Build.VERSION.Incremental);
            errorReport.Append(LINE_SEPARATOR);
            Intent intent = new Intent(myContext, typeof(AnotherActivity));
            intent.PutExtra("error", errorReport.ToString());
            myContext.StartActivity(intent);

            Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
            System.Environment.Exit(0);
        }

    }

And, In Activity,

public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            Java.Lang.Thread.DefaultUncaughtExceptionHandler = new ExceptionHandler(this);
           
             Java.Lang.Integer.ParseInt("sjbbdhcbdc"); // for raising the exception.
           
        }

       
    }



however, below method is never executed, when exception is raised.

 public void UncaughtException(Thread thread, Throwable throwable)
                {
                        // ...
                }

please help.
Umashankar.
Loading...