catching exceptions

B

bitshift

In many asp.net pages I want to trap for errors, but I often really dont do
anything with the exception object itself, so I get annorying warning
messages when it comes time to build. So i started changing how i catch
exceptions like so:

try {

....code here
}
catch(Exception) {

//do something here
}

Notice im not creating an explicit exception object, as is the usual cas
like so:
catch (Exception ex) ...

Is it bad (performance wise) to NOT create an explicit exception object like
im doing ?
 
N

Niels Ull

In many asp.net pages I want to trap for errors, but I often really
dont do anything with the exception object itself, so I get annorying
warning messages when it comes time to build. So i started changing
how i catch exceptions like so:

try {

...code here
}
catch(Exception) {
//do something here
}
Notice im not creating an explicit exception object, as is the usual
cas
like so:
catch (Exception ex) ...
Is it bad (performance wise) to NOT create an explicit exception
object like im doing ?

If you don't refer to ex, there is no difference what so ever - the generated
code will be exactly the same.
Leaving out ex is just a syntactical way of indicating you won't use it.

By the way, it's very bad style to just eat exceptions like this - it makes
it extremely hard to locate errors.
 
P

Peter Duniho

bitshift said:
[...]
Is it bad (performance wise) to NOT create an explicit exception object like
im doing ?

No. In fact, if you don't actually need access to the data within the
Exception, it's a perfectly normal thing to do.

I'd like to temper Niels suggestion that "eating exceptions" is a bad
thing to do, by asking whether the code you posted is literally how you
are catching the exception. Is it?

If you are catching the base Exception class, then you definitely need
to get the exception instance itself, look at it, and if it's not a kind
of exception that you expected and know how to deal with, rethrow it.

If, on the other hand, the code you posted is just "for example" and in
reality you are catching a specific type of exception, then you should
be fine. Just make sure that you don't accidentally catch exceptions
that you didn't expect to occur at that point in the code (if you want a
general-purpose catch-all clause, that's fine, but you should treat that
as a severe error and always display some information to the user
reporting the error).

Pete
 
P

Patrice

What do you do in the catch clause ? Are you after the finally clause that
allows to perform guaranteed cleanup ?

You may want to explain what you are trying to do (having a global exception
handler that centrally report errors and do only some cleanup at particular
places when needed ???)
 
B

Bond

In many asp.net pages I want to trap for errors, but I often really dont do
anything with the exception object itself, so I get annorying warning
messages when it comes time to build.So i started changing how i catch
exceptions like so:

U need to check whether u had implemented the System.Exception; has
been mentioned or not.
and if still the problem arises the best and the most effective way is
to use debugging functions .
the only thing u need to do is that just select the statement which
rise the error and debug it .
It will show u the show u the execution of program as well as Error
too.....

thank you.

Jitesh tolar
http://www.intelcs.com/IT-Companies
 

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