Difference between try{}catch{} and try{}catch(Exception e){} ?

M

Mr Flibble

Are

try
{
//something
}
catch
{
//
}

And

try
{
//something
}
catch (Exception e)
{
//
}

The same ? The first one will catch all exceptions and the second will
catch all exceptions of type Exception which since it's the base type of
all Exceptions will catch everything.

So are they one and the same thing?
 
C

ChrisM

Mr Flibble said:
Are

try
{
//something
}
catch
{
//
}

And

try
{
//something
}
catch (Exception e)
{
//
}

The same ? The first one will catch all exceptions and the second will
catch all exceptions of type Exception which since it's the base type of
all Exceptions will catch everything.

So are they one and the same thing?

The way you have written it, yes.
Btw, both examples as they stand will obvioulsy just 'lose' the exception...
The advantage of having 'catch (Exception e)' is that you can write code to
inspect the Exception object, and make decisions accordingly.
You do realise that you can have multiple 'catch' block for the same 'try'
block to catch and handle different sorts of Exception?

Cheers,

Chris.
 
M

Mr Flibble

* ChrisM said:
The way you have written it, yes.
Btw, both examples as they stand will obvioulsy just 'lose' the exception...
The advantage of having 'catch (Exception e)' is that you can write code to
inspect the Exception object, and make decisions accordingly.
You do realise that you can have multiple 'catch' block for the same 'try'
block to catch and handle different sorts of Exception?

Yes I do. I was just unsure of what the difference between catch, and
catch(Exception e) since they both catch any exception. I will actually
be using catch(MyException e), catch(MyException2 e) so my question was
just theoretical.

Flibble
 
J

Joanna Carter [TeamB]

"Mr Flibble" <[email protected]> a écrit dans le message de
[email protected]...
| Are
|
| try
| {
| //something
| }
| catch
| {
| //
| }
|
| And
|
| try
| {
| //something
| }
| catch (Exception e)
| {
| //
| }
|
| The same ? The first one will catch all exceptions and the second will
| catch all exceptions of type Exception which since it's the base type of
| all Exceptions will catch everything.

As Chris says, yes, they are both the same, but I might add that they are
both fairly useless.

You should always catch exceptions specific to the code that is being
executed in the try block. The application's default exception handler will
normally handle anything else that is not specifically caught in your catch
block.

By catching all exceptions, you could end up silencing some exceptions that
would normally be handled elsewhere.

Joanna
 
J

Jon Skeet [C# MVP]

Mr said:

The same ?

Not quite. It's possible for non-Exceptions to be thrown, although not
from "proper" managed code, I believe. I've never personally run into a
situation where anything other than Exception is thrown, but it's
possible. When you catch Exception, such situations wouldn't get
caught.

I don't know the details of when non-Exceptions can be thrown, but
that's the gist of it.

Jon
 
B

Barry Kelly

Jon Skeet said:
Not quite. It's possible for non-Exceptions to be thrown, although not
from "proper" managed code, I believe. I've never personally run into a
situation where anything other than Exception is thrown, but it's
possible. When you catch Exception, such situations wouldn't get
caught.

I don't know the details of when non-Exceptions can be thrown, but
that's the gist of it.

It's no longer possible to catch a non-Exception in C# with catch{} in
..NET 2 unless you set an attribute (RuntimeCompatibilityAttribute) on
your assembly to enable it. Now, the CLR will wrap any non-Exception
exceptions in a RuntimeWrappedException.

The CLI spec says that any object may be thrown (at the IL level). The
CLS requires that any non-Exception exceptions get translated into
Exception exceptions before they're exposed to the CLS interface, as it
were, for language interoperability. For .NET 2, the CLR does this
automatically when the exception crosses an assembly boundary, and the
assembly isn't marked with the above attribute. (At least I think that's
the mechanism used; I paid attention when it was being discussed in the
blogs and MSDN Feedback site, during the betas.)

-- Barry
 
T

Tom Spink

Mr said:
Yes I do. I was just unsure of what the difference between catch, and
catch(Exception e) since they both catch any exception. I will actually
be using catch(MyException e), catch(MyException2 e) so my question was
just theoretical.

Flibble

Hi Flibble,

Without all the non-exception hoo-hah, the difference is that without
defining a variable to hold the exception, you can't access the exception
that was thrown. You can also catch an exception without assigning it to a
locally-scoped variable:

try
{
...
}
catch ( InvalidOperationException )
{
}
catch ( ArgumentNullException )
{
}

So you won't be able to access the exception object itself, but the correct
catch clause will be executed up on the associated exception being thrown.

Hope this helps,
-- Tom Spink
 
J

Jon Skeet [C# MVP]

Barry said:
It's no longer possible to catch a non-Exception in C# with catch{} in
.NET 2 unless you set an attribute (RuntimeCompatibilityAttribute) on
your assembly to enable it. Now, the CLR will wrap any non-Exception
exceptions in a RuntimeWrappedException.

That's good to know. It makes a lot more sense that way :)

Jon
 

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

Similar Threads

Try Catch Exception 14
catch exceptions 2
Exception 5
Exception 3
try/catch in C# 2
Which kind of exception ? 5
try-catch question 5
Find Specific Exception to Catch. 3

Top