Bug 11087 - Exception.Message is incorrect at runtime (but correct in the debugger watch window)
Summary: Exception.Message is incorrect at runtime (but correct in the debugger watch ...
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.8.x
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2013-03-12 18:18 UTC by Gonz
Modified: 2017-06-28 21:51 UTC (History)
4 users (show)

Tags: bb clb
Is this bug a regression?: ---
Last known good build:

Sample app illustrating the bug (1.06 MB, application/octet-stream)
2013-03-12 18:18 UTC, Gonz
Output log for my attached project (3.48 KB, text/plain)
2013-03-12 18:19 UTC, Gonz

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Gonz 2013-03-12 18:18:24 UTC
Created attachment 3584 [details]
Sample app illustrating the bug

Consider this code:

    public void IsolatedBug() {
      try {
        var adapter = BluetoothAdapter.DefaultAdapter;
        var device = adapter.BondedDevices.Cast<BluetoothDevice>().First();
        UUID uuid = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
        var socket = device.CreateRfcommSocketToServiceRecord(uuid);
        socket.Connect();  <-- "Service discovery failed" IOException occurs here
      catch (Exception ex) {
        string message = ex.Message;
        Log.Info("ExceptionBug", "Message: " + message);
        Log.Info("ExceptionBug", "Callstack: " + ex.ToString());

In the debugger (both Xamarin Studio and Visual Studio), the watch window correctly shows the ex.Message property string as "Service discovery failed".

However, when the property is evaluated by the application, it incorrectly sees the value "Exception of type 'Java.IO.IOException' was thrown."  Thus, this incorrect string is printed to the log, not the expected correct string "Service discovery failed".

It appears that IOException overrides the virtual property Exception.Message, but the Xamarin runtime is incorrectly executing the base class implementation.

I'm attaching an output log and source code for a simple project that isolates the bug.  (To actually run my project, note that you would need a paired Bluetooth device.)
Comment 1 Gonz 2013-03-12 18:19:30 UTC
Created attachment 3585 [details]
Output log for my attached project

I also pasted the Xamarin Studio about box text into the log file.
Comment 2 Stephen Feest 2014-09-29 13:01:49 UTC
I am experiencing exactly the same issue with an exception of type Android.Accounts.AuthenticatorException. This exception is created by the Android AccountManager based on an error code and error description returned by an Authenticator.

If I call .Message on the property in the Immediate window I get the correct exception message (in my case "Services are unavailable until 12:00"), however exactly the same line of code at runtime returns "Exception of type 'Android.Accounts.AuthenticatorException' was thrown."
Comment 3 Cody Beyer (MSFT) 2017-06-28 21:51:26 UTC
In testing on the latest version, ex.Message and the log report the same error message