Empty catch

  • Thread starter Thread starter DaTurk
  • Start date Start date
DaTurk said:
What's the CLI equivalent of the empty catch?

i.e.
try{}
catch
{}

How about this:

try
{
Console::WriteLine(L"Hello World");
}

catch ( Exception^ )
{
}

Regards,
Will
 
DaTurk said:
What's the CLI equivalent of the empty catch?

i.e.
try{}
catch
{}

Where is that empty catch allowed? It is not standard C++. Do you mean
catch(...) ?
 
SvenC said:
Where is that empty catch allowed? It is not standard C++. Do you mean
catch(...) ?

It's allowed in C#. It's equivalent to the C++/CLI construct:

try
{
}
catch(Exception^)
{
}

Using catch(...) will also catch native exceptions, and possibly structured
(platform) exceptions, depending on the compiler options used.

-cd
 
Hi Carl!
It's allowed in C#. It's equivalent to the C++/CLI construct:

try
{
}
catch(Exception^)
{
}

No. "catch(Exception^)" is *not* the same as "catch" in C#!

"catch(Exception^)" will *only* catch exceptions which are derived from
"System.Exception"!

"catch" will catch *all* Exceptions (derived from "System.Object")!
It also will catch native exceptins in C#!!!


Therefor the C++/CLI equivalent of "catch" is "catch(System::Object^)" !



See also: Basic Concepts in Using Managed Exceptions
http://msdn2.microsoft.com/en-us/library/df24ysb6(VS.80).aspx


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Jochen Kalmbach said:
Hi Carl!


No. "catch(Exception^)" is *not* the same as "catch" in C#!

"catch(Exception^)" will *only* catch exceptions which are derived from
"System.Exception"!

"catch" will catch *all* Exceptions (derived from "System.Object")!
It also will catch native exceptins in C#!!!

Egads!

I'd forgotten that the CLR actually supporting throwing something not
derived from System.Exception!

-cd
 
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?
 
Hi Ben!
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
 
Jochen Kalmbach said:
Hi Ben!

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:

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?
 
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
 
I wasn't doubting that catching System.Object will catch all exceptions...
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...


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
 
Back
Top