By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Jun 22 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Mr Flibble" <mr********@flibbyly.wobbly.n.et> wrote in message
news:e7***********@custnews.inweb.co.uk...
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.
Jun 22 '06 #2

P: n/a
* ChrisM wrote:
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
Jun 22 '06 #3

P: n/a
"Mr Flibble" <mr********@flibbyly.wobbly.n.et> a écrit dans le message de
news: e7***********@custnews.inweb.co.uk...
| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 22 '06 #4

P: n/a
Mr Flibble wrote:
Are
<snip - catch{} and catch(Exception){} >
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

Jun 22 '06 #5

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Mr Flibble wrote:
Are


<snip - catch{} and catch(Exception){} >
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.


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

--
http://barrkel.blogspot.com/
Jun 22 '06 #6

P: n/a
Mr Flibble wrote:
* ChrisM wrote:
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


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
Jun 22 '06 #7

P: n/a
Barry Kelly wrote:
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.


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

Jon

Jun 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.