Silly question about Try Catch...

G

Guest

Hello I know this is extremely basic, I just want to make sure I got it right.

is:

try{}
catch{}

the same as

try{}
catch(System.Exception e){}

I mean if I am not using the reference to the exception then I should simply
use
catch{}? or is catch(System.Exception){} somehow more restricted on what it
catches than simply catch{}?

Thanks in advance

JT.
 
A

adebaene

John a écrit :
Hello I know this is extremely basic, I just want to make sure I got it right.

is:

try{}
catch{}
This is not valid C++ : catch block must have an exception type
specification (see compiler error C2309). You must be confusing with C#
where this syntax is valid.

Arnaud
MVP - VC
 
G

Guest

The way to make a catch block unrestrictive is to use the elipsis as the
parameter. That is:

try{}
catch(...){}

that means catch anything that is thrown, no matter what it is. This is
very useful if you need to perform some manual clean-up operation when an
exception is thrown... you can send the exception on with the throw command
without passing it a parameter... as such:

try{}
catch(...)
{
// perform cleanup here
throw;
}

but if you do:
try{}
catch(System::Exception* ex){}

then you are saying that you only want to catch exception that are derived
from System::Exception. Any exception that is thrown that does not derive of
this object will not be caught in this exception (with managed code, all
exceptions should derive from System::Exception).

Or, perhaps you are refering to this type of syntax:

try{}
catch(System::Exception*){}

this says that you want to catch any exception that derives from
System::Exception, but you do not care to use the object

hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top