Hi
I'm using Compact Framework 3.5 / VS2008. I'm getting really odd behavior with TypeLoadException. The following code throws this error. The reason is a problem with the database connection. However for some unknown reason this inner exception is lost and is not contained in the TypeLoadException.
try
{
settingsFromDb = SettingsFromDbManager.Instance;
}
catch (Exception ex)
{
throw ex; // BREAKPOINT HERE
}
If we look at the SettingsFromDbManager class below it can be seen that it is a simple singleton class. The database error is occurring in the Load() method. I haven't included this code in the sample. If I put a breakpoint at the position indicated in the sample below I can see a database error. Unfortunately if I put a breakpoint in the position indicated in the code above then all I get is the TypeLoadException with no inner exception. There is nothing to indicate that a database problem occurred. This is bad :( Does anyone know why this strange behavior could be happening??
Cheers
Mark
public sealed class SettingsFromDbManager
{
static readonly SettingsFromDbManager _instance = new SettingsFromDbManager();
SettingsFromDbManager()
{
try
{
Load();
}
catch (Exception ex)
{
throw ex; // BREAKPOINT HERE
}
}
public static SettingsFromDbManager Instance
{
get
{
return _instance;
}
}
.... more code ...
}
** Update **
Thanks very much for all the great suggestions and help!
Pierre I used the test class you so kindly wrote. Here's the code I called it with. It must be a quirk of the Compact Framework I guess because when I examined the exception it was TypeLoadException with no inner exception :(
try
{
Fail.Test();
}
catch (Exception ex)
{
var x = ex.ToString(); // BREAKPOINT HERE
}
I think VinayC is probably correct about the reason. This is all a bit beyond my knowledge. Not sure what to do now. I don't want to give up my Singleton classes - they are useful. I'm using the "fourth version" Singleton pattern from http://csharpindepth.com/Articles/General/Singleton.aspx. I haven't used them before but seemed like a good idea to share the same instance of some utility classes around the application rather than creating and disposing them numerous times. Performance is a big issue with the Compact Framework.
* Update *
WOO HOO! All I had to do was change the Singleton class as follows. It instantiates the class in the property getter. Now my exceptions bubble to the surface as expected :)
public sealed class SettingsFromDbManager
{
static SettingsFromDbManager _instance = null;
SettingsFromDbManager()
{
try
{
Load();
}
catch (Exception ex)
{
throw new Exception("Error loading settings", ex);
}
}
public static SettingsFromDbManager Instance
{
get
{
if (_instance == null)
_instance = new SettingsFromDbManager();
return _instance;
}
}
.... more code ...
}