08-22-2013 03:15 PM
I'm using a 3rd-party .NET library that throws .NET Exceptions for errors. As it turns out, the Message property of the Exception does *not* contain the useful information (the developer of the library gave me the technical reason for this, it's due to the remoting wrapper, but that's not relevant).
What I need to do is the Test Stand equivalent of calling ToString() on the Exception instance. For example, the below C# snippet shows what I need to do:
try {
library.Function();
}
catch (Exception e)
{
// This is what I need to do in TestStand, it gets me the necessary error text
Console.WriteLine(e.ToString()); // Note: the 'e.ToString()' is the default, this could just be (e)
// This is what TestStand currently does, which loses necessary error text
Console.WriteLine(e.Message);
}
Does anybody have a suggestion on how to make Test Stand *not* use the 'Message' property but to instead use the ToString() method?
Thanks in advance!
Steve
08-23-2013 04:52 PM - edited 08-23-2013 04:54 PM
You will need to write a wrapper function around it in your own assembly that catches and converts the exception.
As a side note, if the issue is that the exception does not support remoting and is being passed across appdomain boundries, the developer of the library you are trying to call can fix that by making their exception classes serializable (they might not be aware of how to do this). Here is an example (NOTE: This example is done in C++, but the same thing can be done in C#):
[Serializable] public ref class ManagedTEException : public Exception, public System::Runtime::Serialization::ISerializable { public: ManagedTEException(System::String^ description, System::Int32 errorCode) : Exception(description) { mErrorCode = errorCode; } ManagedTEException(System::String^ description, System::Int32 errorCode, System::Exception^ innerException) : Exception(description, innerException) { mErrorCode = errorCode; } property System::Int32 ErrorCode { System::Int32 get() { return mErrorCode; } } // Needed so that deserialize works when passing object across appdomain boundries. [System::Security::Permissions::SecurityPermissionAttribute (System::Security::Permissions::SecurityAction::LinkDemand, Flags=System::Security::Permissions::SecurityPermissionFlag::SerializationFormatter)] virtual void GetObjectData(System::Runtime::Serialization::SerializationInfo^ info, System::Runtime::Serialization::StreamingContext context) override { info->AddValue("ManagedTEException.mErrorCode", mErrorCode); // Let base class do its work. Exception::GetObjectData(info, context); } protected: // Needed so that deserialize works when passing object across appdomain boundries. [System::Security::Permissions::SecurityPermissionAttribute (System::Security::Permissions::SecurityAction::LinkDemand, Flags=System::Security::Permissions::SecurityPermissionFlag::SerializationFormatter)] ManagedTEException(System::Runtime::Serialization::SerializationInfo^ info, System::Runtime::Serialization::StreamingContext context) : Exception(info, context) { mErrorCode = info->GetInt32("ManagedTEException.mErrorCode"); } private: System::Int32 mErrorCode; };
Hope this helps,
-Doug