Hi Ben!
>> It also will catch native exceptins in C#!!!
>
> Wouldn't catch (Exception^) do so as well, giving an SEHException? Or is
> that only for p/invoke?
SEHException is also derived from "System.Exception". But you can throw
*any* object as an exception. Therefor, if you want to catch *all*
exceptions, you need to catch "System.Object" instead of "System.Exception"!
See:
#pragma unmanaged
void Test1()
{
throw 1;
}
#pragma managed
void Test2()
{
throw 2;
}
void Test3()
{
throw gcnew System::Object();
}
int main()
{
try
{
Test1();
}
catch(System::Object ^o)
{
System::Console::WriteLine("Test1: " + o->GetType()->ToString());
}
try
{
Test2();
}
catch(System::Object ^o)
{
System::Console::WriteLine("Test2: " + o->GetType()->ToString());
}
try
{
Test3();
}
catch(System::Object ^o)
{
System::Console::WriteLine("Test3: " + o->GetType()->ToString());
}
}
Output is:
Test1: System.Runtime.InteropServices.SEHException
Test2: System.Runtime.InteropServices.SEHException
Test3: System.Object
Greetings
Jochen
Ben Voigt - 23 May 2007 14:19 GMT
> Hi Ben!
>>> It also will catch native exceptins in C#!!!
[quoted text clipped - 8 lines]
>
> See:
I wasn't doubting that catching System.Object will catch all exceptions...
but wouldn't catching System.Exception also catch all exceptions, decoding
managed objects derived from Exception, and wrapping everything else in
SEHException?
> #pragma unmanaged
>
[quoted text clipped - 52 lines]
> Greetings
> Jochen
Jochen Kalmbach [MVP] - 23 May 2007 14:33 GMT
Hi Ben!
> I wasn't doubting that catching System.Object will catch all exceptions...
> but wouldn't catching System.Exception also catch all exceptions, decoding
> managed objects derived from Exception, and wrapping everything else in
> SEHException?
I don't understand the background of this question?
I don't know the internals of every assembly; so I don't know if
"System.Exception" will catch *all* exceptions... regardless of
SEHException...
Greetings
Jochen
Mattias Sjögren - 23 May 2007 19:37 GMT
>> I wasn't doubting that catching System.Object will catch all exceptions...
>> but wouldn't catching System.Exception also catch all exceptions, decoding
[quoted text clipped - 6 lines]
>"System.Exception" will catch *all* exceptions... regardless of
>SEHException...
You can also use the RuntimeCompatibilityAttribute on your assembly
with WrapNonExceptionThrows = true to make sure all exceptions you see
are derived from Exception (if they aren't already, they get wrapped
in a RuntimeWrappedException). That's what C# does for example.
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.