473,804 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Exceptio n whenever possible; try to catch just the
specific errors that are specific to that block of code. Catch
System.IO.FileN otFound instead."

Can someone elaborate on this?

Thanks in advance!
Nov 19 '06 #1
2 1206
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.Curre ntDomain.Unhand ledException handing

p"Always try to catch specific exceptions.
p>
pAvoid catching System.Exceptio n whenever possible; try to catch just
pthe specific errors that are specific to that block of code. Catch
pSystem.IO.File NotFound 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.Unhan dledException 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.Exceptio n whenever possible; try to catch just the
specific errors that are specific to that block of code. Catch
System.IO.FileN otFound instead."

Can someone elaborate on this?
If you know a method might throw FileNotFoundExc eption 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 FileNotFoundExc eption. 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
FileNotFoundExc eption due to concurrency issues, so you should still use try..catch in this case.
Then, FileNotFoundExc eption 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 OutOfMemoryExce ption, StackOverflowEx ception and
ExecutionEngine Exception 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 "constraine d execution regions", for example)

Other exceptions such as ArgumentNullExc eption 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******** *******@TK2MSFT NGP02.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.Exceptio n whenever possible; try to catch just the
specific errors that are specific to that block of code. Catch
System.IO.FileN otFound 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
1361
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 happening? I am using the production = version of mySQL 4.0.13 in windows 2000. Here is the query: create temporary table tmp (
2
2106
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 questions? I haven't been able to find a mailing list or newsgroup specifically dealing with the MySQL C API. -- Med venlig hilsen / Best regards
7
1971
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 particular cell or column is, how wide a particular font is, and so on.
162
14934
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 choice questions.etc.. I'll be indebted to everyone.. Thanks in advance.. regards vasant shetty Bangalore
8
1616
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 (from specific to more general): 1. Having looked though some C sources I was horrified by the number of macros. E.g., what could be the purpose of using this: #define CFG_REF(c, f) ((c) -> f) #define SERVER_ADDRESS(c) CFG_REF (c, server_address)
6
1240
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 build an admin page so someone can create the questions and store them on SQL Server. The responses from those questions need to then be stored on the database. Is this possible or is it just a pipe dream? If possible, is it so complicated that...
10
1182
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 expected when I open the form the default value is set to today's date. Now I sometimes also call Form2 from Form1. Ok now here is my problem. Actually when I call Form2 from within Form1 I want to set the set the value of datetimepicker control to...
28
25517
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 A { int n; int m;
13
1628
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
1621
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. What kinds of questions would you advice to ask the person on the interview to see if he/she is: 1. Proficient with php 2. Proficient with MySQL (development, maybe some administration) 3. Has web app architecture skills and knows how to build...
0
9710
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.