|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Is this a bug in Exception.Source?v1.1 of the framework. My application uses the Exception EAB, which displays the source as part of the logging. I've been refactoring my code to throw exceptions insted of returning error codes which had an issue with being ignored, and when I have the EAB log an exception, Exception.get_Source is throwing a NullReferenceException. Below is a case that will cause that exception, just create a managed c++ console app, and paste this in for _tmain. #include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() { Console::WriteLine(S"Starting unmanaged Test App"); try { throw new System::Exception("Foo"); } catch( System::Exception* ex ) { Console::WriteLine(S"Exception Caught, Getting ready to display the Source:"); //The next line throws an exception Console::WriteLine(ex->Source); } return 0; } This test app errors out with a null ref exception. I reflected the property, the work is done in these lines: //C# Code, from System.Exception.get_Source() StackTrace trace1 = new StackTrace(this, true); if (trace1.FrameCount > 0) { StackFrame frame1 = trace1.GetFrame(0); System.Reflection.MethodBase base1 = frame1.GetMethod(); System.Type type1 = base1.DeclaringType; //Executing this line causes an exception to be throw. System.Reflection.Module mod = type1.Module; System.Reflection.Assembly asm = mod.Assembly; //string _source = asm.nGetSimpleName(); } If I execute these lines (substituting an exception for 'this' in StrackTrace's constructor) The exception occurs on the marked line. Now at least I think it's because I'm throwing the exception from an unmanaged class I read up, and I think the problem is base1.DeclaringType can return null, but there's no checks to see if that property returned null. from the MSDN: "If the MemberInfo object is a global member, (that is, it was obtained from Module.GetMethods, which returns global methods on a module), then the returned DeclaringType will be a null reference (Nothing in Visual Basic)." I'm planning on converting the class that throws the exception to a managed class to avoid this exception, are there any other suggestions? Maybe before passing the exception into the EAB, I can try to access the Source property, and if it throws an exception, set the source to "Unmanaged Code" or something wierd like that. Kunnis |
|||||||||||||||||||||||