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

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

Post by Mr Flibbl » Fri, 23 Jun 2006 18:18:45


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?
 
 
 

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

Post by Chris » Fri, 23 Jun 2006 18:44:42


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.

 
 
 

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

Post by Mr Flibbl » Fri, 23 Jun 2006 18:52:04


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
 
 
 

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

Post by Joanna Car » Fri, 23 Jun 2006 18:54:56

"Mr Flibble" < XXXX@XXXXX.COM > a rit dans le message de

| 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
 
 
 

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

Post by Jon Skeet » Fri, 23 Jun 2006 20:00:36


<snip - catch{} and catch(Exception){} >


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
 
 
 

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

Post by Barry Kell » Fri, 23 Jun 2006 21:49:40


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://www.yqcomputer.com/
 
 
 

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

Post by Tom Spin » Fri, 23 Jun 2006 22:24:37


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
 
 
 

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

Post by Jon Skeet » Sat, 24 Jun 2006 00:25:01


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

Jon