Exceptions when using Xamarin Android

I recently started learning mobile development with Xamarin Android. I am using VS 2012. I noticed that when I open the android project, all exceptions from Debug → Exceptions are not marked. I thought the reason was that the exceptions introduced into the code were not shown the way I was used to developing desktop systems. When I checked the exceptions in the Debug-> Exceptions window and tried to deploy the solution for the emulator, it failed - there were no errors, but the application did not start in the emulator.

So my question is: is this normal behavior when developing for Android using VS 2012 or VS 2010 and the emulator? Is there a way to see thrown exceptions in the “usual way” not only in the output window. Will this change if I use the actual Android device for debugging?

+6
android c # visual-studio-2012
source share
4 answers

Perhaps I was not clear enough. The execution of the code in visual studio really breaks, but the instad of the standard window used to examine the exception that I get when programming for the desktop, the window contains only the name of the exception being thrown, a checkbox to disable code breaking when this type of exception and Break buttons. Continue and Ignore. But I can not investigate the actual exception in a way that is available when programming on the desktop.

In the Debug --> Exceptions dialog box, if you select the Thrown check box for the Common Language Runtime Exceptions , you will get a more meaningful description of the excluded solution.

enter image description here

Obviously, the other big thing is to click the Break button and see what is in the call stack.

+5
source share

My answer is NO

Why?

There is one important thing that you must understand about the nature of the unhandled exception in Android, there is nobody .... on Android this is an Uncaught exception, which means that you cannot “handle” it or repair it, as you might in .NET environment

Xamarin (Mono) internally "handles" these uncaught exceptions, literally surrounding everyone with an attempt to catch the Unhandled event, but this is irrelevant. It is also discouraged for interacting with the user interface for various reasons.

Theoretically, there are several "workarounds" for displaying a dialog for the user or restarting the application, none of which I would recommend doing. Instead, you should surround sensitive areas with try-catch clauses to handle expected exceptions, as for the unexpected, just use the exception reporting component and update the application after analyzing the registered exceptions

Explanation From

You can also catch an unreviewed exception from the application. Like this

Create a basic activity, for example the name ErrorActivity

look at this example

 protected override void OnCreate(Bundle bundle) { //register error handlers AppDomain.CurrentDomain.UnhandledException += ErrorHandler.CurrentDomainOnUnhandledException; TaskScheduler.UnobservedTaskException += ErrorHandler.TaskSchedulerOnUnobservedTaskException; } 

In the error handler class

 public static class ErrorHandler { /// <summary> /// Tasks the scheduler on unobserved task exception. /// </summary> /// <param name="sender">The sender.</param> /// <param name="unobservedTaskExceptionEventArgs"> /// The <see cref="UnobservedTaskExceptionEventArgs" /> instance containing /// the event data. /// </param> public static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs) { var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception); LogUnhandledException(newExc); } /// <summary> /// Currents the domain on unhandled exception. /// </summary> /// <param name="sender">The sender.</param> /// <param name="unhandledExceptionEventArgs"> /// The <see cref="UnhandledExceptionEventArgs" /> instance containing the event /// data. /// </param> public static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) { var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception); LogUnhandledException(newExc); } /// <summary> /// Logs the unhandled exception. /// </summary> /// <param name="exception">The exception.</param> internal static void LogUnhandledException(Exception exception) { try { string error = $"Exception Caught:{DateTime.Now:F} The Error Message IS {exception.Message}\n\r full stack trace is {exception.ToString()} "; #if DEBUG const string errorFileName = "errorlog.txt"; var libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources var errorFilePath = System.IO.Path.Combine(libraryPath, errorFileName); System.IO.File.WriteAllText(errorFilePath, error); Android.Util.Log.Error("Crash Report error not handled", ex.ToString()); #else // Log to Android Device Logging. Android.Util.Log.Error("Crash Report", error); #endif } catch (Exception ex) { Android.Util.Log.Error("Crash Report error not handled", ex.ToString()); // just suppress any error logging exceptions } } } 

Now you can inherit all actions from ErrorActivity like this

 Public class Fooactivity:ErrorActivity { } 

Now you can handle the error in the application ... Get the error log from the File .. log or the Android device registration monitor. Hope this helps ...

+5
source share

I am also new to Xamarin. The simple answer is: no, this is not what you should expect.

If I get an exception when the application runs on the device, it is interrupted in Visual Studio. It is best to contact Xamarin directly or go to the forums.

0
source share

I managed to dig out the Unhandled exception:

Do the following:

  • Event catch AppDomain.CurrentDomain.UnhandledException
  • If you look carefully, you will see that it was basically somehow translated by the Java.Lang.Runtime exception, so if you examine the ExceptionObject property, it should give you a very clear message about what is wrong with the application. In my case, it gave me a very clear message that I did not name the OnDestroyView base in my Fragment
0
source share

All Articles