473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why shouldn't I catch general exceptions?

FxCopy says this about catching general exceptions:

"You should not catch Exception or SystemException.
Catching generic exception types can hide run-time
problems from the library user, and can complicate
debugging. You should catch only those exceptions that
you can handle gracefully."

This is for a winform app but applies to asp.net also. There are some
code blocks you may know which exception may get thrown and others
that you are unsure. I guess FxCop is saying to just put in several
catches for specific exceptions. However, what if you guess wrong?

I currently write general exceptions into the Event Log. Why would
that not be a good way to go?

I'm not familiar with coding a robust try/catch/finally blocks. If
you put in the specific exception, say IOException, and your code
block happens to throw and argument exception, what then? If you
didn't do anything anywhere else, that goes straight up to the user
and crashes the app (winforms). What are the recommendations for
handling this?

Thanks,
John

Mar 9 '07 #1
6 1916
My personal opinion is that you should catch all exceptions gracefully, but
make sure the user is informed that an error has occured. The reason it
states it speciifically for libraries is likely to be that a library is an
object thats consumed, so its up to the consumer to determine what to do
with any thrown error. With a library, its normally a coder developing code
that uses the library, so they should handle any exception thrown to them.
Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"john_c" <jo***@bigstring.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
FxCopy says this about catching general exceptions:

"You should not catch Exception or SystemException.
Catching generic exception types can hide run-time
problems from the library user, and can complicate
debugging. You should catch only those exceptions that
you can handle gracefully."

This is for a winform app but applies to asp.net also. There are some
code blocks you may know which exception may get thrown and others
that you are unsure. I guess FxCop is saying to just put in several
catches for specific exceptions. However, what if you guess wrong?

I currently write general exceptions into the Event Log. Why would
that not be a good way to go?

I'm not familiar with coding a robust try/catch/finally blocks. If
you put in the specific exception, say IOException, and your code
block happens to throw and argument exception, what then? If you
didn't do anything anywhere else, that goes straight up to the user
and crashes the app (winforms). What are the recommendations for
handling this?

Thanks,
John

Mar 9 '07 #2
John,
It just means to try to catch specific ones first. The more specific
an error you can catch then the more useful your response to the user will
be. For example, if you were creating a user and something went wrong, you
would want to let the user know that there was an issue with their password,
or that a user had already registered with that username as opposed to a
real failure due to the database being offline. Catching general exceptions
is fine, but usually after you've tried to catch the more specific
exceptions to the code you are writing.

Keep in mind, you'll want to avoid lots of nested try/catch blocks
unless where necessary as each try/catch block will result in a slight
performance hit. In high-volume sites this can become more noticeable.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"john_c" <jo***@bigstring.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
FxCopy says this about catching general exceptions:

"You should not catch Exception or SystemException.
Catching generic exception types can hide run-time
problems from the library user, and can complicate
debugging. You should catch only those exceptions that
you can handle gracefully."

This is for a winform app but applies to asp.net also. There are some
code blocks you may know which exception may get thrown and others
that you are unsure. I guess FxCop is saying to just put in several
catches for specific exceptions. However, what if you guess wrong?

I currently write general exceptions into the Event Log. Why would
that not be a good way to go?

I'm not familiar with coding a robust try/catch/finally blocks. If
you put in the specific exception, say IOException, and your code
block happens to throw and argument exception, what then? If you
didn't do anything anywhere else, that goes straight up to the user
and crashes the app (winforms). What are the recommendations for
handling this?

Thanks,
John

Mar 9 '07 #3
"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:lp******************************@eclipse.net. uk...
My personal opinion is that you should catch all exceptions gracefully,
but make sure the user is informed that an error has occured. The reason
it states it speciifically for libraries is likely to be that a library is
an object thats consumed, so its up to the consumer to determine what to
do with any thrown error. With a library, its normally a coder developing
code that uses the library, so they should handle any exception thrown to
them.
Yes indeed...

Generally speaking, I catch all exceptions in base classes because I don't
want a crash, but don't handle them there - I simply throw them back to the
calling method which decides what to do next...
Mar 9 '07 #4
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:u3**************@TK2MSFTNGP04.phx.gbl...
"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:lp******************************@eclipse.net. uk...
>My personal opinion is that you should catch all exceptions gracefully,
but make sure the user is informed that an error has occured. The reason
it states it speciifically for libraries is likely to be that a library
is an object thats consumed, so its up to the consumer to determine what
to do with any thrown error. With a library, its normally a coder
developing code that uses the library, so they should handle any
exception thrown to them.

Yes indeed...

Generally speaking, I catch all exceptions in base classes because I don't
want a crash, but don't handle them there - I simply throw them back to
the calling method which decides what to do next...
If you're going to throw it back to the calling method, then why catch it at
all?

John
Mar 10 '07 #5
"John Saunders" <john.saunders at trizetto.comwrote in message
news:Od**************@TK2MSFTNGP02.phx.gbl...
If you're going to throw it back to the calling method, then why catch it
at all?
Because if I don't the app will crash...
Mar 10 '07 #6
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:OW**************@TK2MSFTNGP05.phx.gbl...
"John Saunders" <john.saunders at trizetto.comwrote in message
news:Od**************@TK2MSFTNGP02.phx.gbl...
>If you're going to throw it back to the calling method, then why catch it
at all?

Because if I don't the app will crash...
I'm sorry, but that makes no sense. What you'd be doing by not catching it
is exactly the same as what would happen if you catch it and then rethrow
it.

Perhaps we're not talking about the same thing. Could you provide a code
example?

And please tell us what you mean when you say "the app will crash".

Johnm
Mar 11 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
6
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
8
by: clintonG | last post by:
Could someone provide me with a URL documenting the specific order of exceptions? -- <%= Clinton Gallagher A/E/C Consulting, Web Design, e-Commerce Software Development Wauwatosa, Milwaukee...
3
by: Steve | last post by:
I have some general catch clauses in my app as follows: try { } catch(Exception ex) { } try
6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file, etc...etc...) Or should Try + Catch be used IN PLACE of...
6
by: Jake K | last post by:
I've been reading throught MS article http://support.microsoft.com/kb/306636. I am in the process of learning C# and have a few questions. First, why are the methods (Pause, AddRecord,...)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.