473,402 Members | 2,050 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,402 software developers and data experts.

Questions about an article

"Unless you have a very good reason to catch an exception, DON'T.

Exceptions are supposed to be exceptional, just like the dictionary meaning:
uncommon, unusual. When in doubt, let the calling routine, or the global
exception handler, deal with it. This is the golden rule. The hardest kinds
of exceptions to troubleshoot are the ones that don't even exist, because a
developer upstream of you decided to consume it."

What's "global exception handler"?

If DON'T catch an exception and let the calling routine to handle it, that
means my code still needs to handle it in the calling routine. So, what the
rule gives?

"Always try to catch specific exceptions.

Avoid catching System.Exception whenever possible; try to catch just the
specific errors that are specific to that block of code. Catch
System.IO.FileNotFound instead."

Can someone elaborate on this?

Thanks in advance!
Nov 19 '06 #1
2 1187
Hello pack,

p"Unless you have a very good reason to catch an exception, DON'T.
pExceptions are supposed to be exceptional, just like the dictionary
pmeaning: uncommon, unusual. When in doubt, let the calling routine,
por the global exception handler, deal with it. This is the golden
prule. The hardest kinds of exceptions to troubleshoot are the ones
pthat don't even exist, because a developer upstream of you decided to
pconsume it."
p>
pWhat's "global exception handler"?

AppDomain.CurrentDomain.UnhandledException handing

p"Always try to catch specific exceptions.
p>
pAvoid catching System.Exception whenever possible; try to catch just
pthe specific errors that are specific to that block of code. Catch
pSystem.IO.FileNotFound instead."
p>
pCan someone elaborate on this?

This mean that you need to catch only what do u expect to manage in this
concrete situation and leave other errors to its own to don't miss other
errors that u expect to catch in other places

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Nov 19 '06 #2
Hi,

<snip>
What's "global exception handler"?
An event handler attached to the AppDomain.UnhandledException event, for instance. The event
handler should be registered when the application is started and it will catch all exceptions that
aren't caught higher in the call stack by try..catch blocks.
Avoid catching System.Exception whenever possible; try to catch just the
specific errors that are specific to that block of code. Catch
System.IO.FileNotFound instead."

Can someone elaborate on this?
If you know a method might throw FileNotFoundException and it can be gracefully handled in code
(i.e., there will be no state corruption and the application may move forward if the exception is
caught) then only catch FileNotFoundException. By catching other exceptions you may inadvertently
hide a bug in your code or corrupt the state of your objects, creating different bugs.

In many cases you can alleviate this need by checking first if state or arguments required by the
method are valid before your code even attempts invocation. For instance, using File.Exists() can
be used to prevent File.OpenRead from being called within an invalid file path. This case isn't a
good example, particularly, because it's still possible for OpenRead to throw a
FileNotFoundException due to concurrency issues, so you should still use try..catch in this case.
Then, FileNotFoundException has even more meaning when its caught - The file was there when the code
checked for its existence, but it wasn't there when OpenRead was called (not that this extra
information will help you at all).

If you catch only Exception then exceptions such as OutOfMemoryException, StackOverflowException and
ExecutionEngineException will be caught as well. You probably don't want to swallow these since
they normally indicate that your code isn't designed properly, it contains a bug or there is a bug
in the framework. And you probably can't handle these gracefully anyway (without another exception
being thrown). The 2.0 framework has made considerable improvements over earlier framework versions
in how these exceptions can be handled so that the state of your application or a system's memory
isn't left in a corrupted state. (Search for "constrained execution regions", for example)

Other exceptions such as ArgumentNullException might indicate a bug in your code so you don't
necessarily want to catch that either in most cases. IMO you're always better off checking for null
first.

--
Dave Sexton

"pack" <pa****@comcast.netwrote in message news:%2***************@TK2MSFTNGP02.phx.gbl...
"Unless you have a very good reason to catch an exception, DON'T.

Exceptions are supposed to be exceptional, just like the dictionary meaning:
uncommon, unusual. When in doubt, let the calling routine, or the global
exception handler, deal with it. This is the golden rule. The hardest kinds
of exceptions to troubleshoot are the ones that don't even exist, because a
developer upstream of you decided to consume it."

What's "global exception handler"?

If DON'T catch an exception and let the calling routine to handle it, that
means my code still needs to handle it in the calling routine. So, what the
rule gives?

"Always try to catch specific exceptions.

Avoid catching System.Exception whenever possible; try to catch just the
specific errors that are specific to that block of code. Catch
System.IO.FileNotFound instead."

Can someone elaborate on this?

Thanks in advance!


Nov 19 '06 #3

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

Similar topics

0
by: Degan, George E, JR, MGSVC | last post by:
I am attempting to create a temporary table to do a complex query and I = get an error: error 1044: Access denied for user: '@localhost' to database = 'shopsample' what can I do to keep this from...
2
by: David List | last post by:
I posed a MySQL C API question a few days back here, and got zero answers. I assume it is because this is not the right place to ask such questions. Does anyone here know where I could ask C API...
7
by: Johnny | last post by:
June 2, 2005 Greetings, I have a whole lot of questions that all have to do with the same topic: Width. By "width" I mean how wide a particular Web page is, how wide a table is, how wide a...
162
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
8
by: V.Ch. | last post by:
In near future I can face a prospect of writing some stuff in C. Being a C++ programmer, I've got practically no experience in C. I'd be obliged if someone could answer the following questions...
6
by: dw | last post by:
Hello, all. We're building a Web app where we can have any number of questions, which can contain any type of form element -- check boxes, text fields, radio buttons, dropdowns, etc. We'd like to...
10
by: patang | last post by:
Question 1 I have two forms, let say Form1 and Form2. There is datetimepicker control on Form2. In the Load Event of Form2 I have set the default value of datetimepicker control to NOW. So as...
28
by: WaterWalk | last post by:
Hi, I'm haunted by 2 questions about struct copy. Though I searched the net, but still in confusion. 1. Does struct assignment copies every member including array members? For example, struct...
13
Savage
by: Savage | last post by:
Hi everyone, this is questions and comments thread about this article. If u have any comments or questions please post them here.. Savage
4
by: Andrey | last post by:
Hi, I will be hiring a php guru to help us architect a highly scalable web site/web application; the problem is I am coming from Microsoft .NET world and not too much familiar with the platform....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.