473,779 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rethrowing Exceptions - Best Practice?

If a class has no need for any specific clean up code in a particular catch block is there any benefit to catching and rethrowing exceptions, rather than just letting the original exception bubble up to the calling function

If so what?
Jul 21 '05 #1
2 1724
If you were to do nothing but rethrow the same exception, there is no
benefit. If you were to catch the exception and throw a *different*
exception that better explains the context of the error, that would be
useful.

Suppose you have a helper class that obtains some configuration information
from a file:

public object GetConfigItem(s tring itemName) {
try {
//try and read file here
} catch (IOException e) {
throw new ConfigurationEx ception(string. Format("Could not get
configuration item called '{0}'", itemName), e);
}
}

Now, instead of the client code receiving an IOException, they receive a
more specific exception (ConfigurationE xception). The client code should not
be concerned with how the configuration items are stored. For example, in
the future, you might want to change your app so that this configuration is
stored in a database. That's fine, you would just catch database-specific
exceptions and throw a ConfigurationEx ception instead.

Notice how the ConfigurationEx ception contains the original exception
(IOException) in it.

HTH,
Kent

"Martin Smith" <an*******@disc ussions.microso ft.com> wrote in message
news:9E******** *************** ***********@mic rosoft.com...
If a class has no need for any specific clean up code in a particular catch block is there any benefit to catching and rethrowing exceptions,
rather than just letting the original exception bubble up to the calling
function?
If so what?

Jul 21 '05 #2
Hi Kent

Thanks for your most comprehensive answer

- Martin
Jul 21 '05 #3

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

Similar topics

10
1789
by: Brian Folke Seaberg | last post by:
I was recently browsing a couple of C++ books at the local bookstore. One book called throwing exceptions from constructors a "dubious practice." Another book recommended not throwing exceptions from constructors due to the fact that the destructor for the object being constructed will not be executed and that as a result any resources allocated by the constructor prior to throwing the exception will not be deallocated if the...
22
2740
by: Drew | last post by:
How do I know which exceptions are thrown by certain methods? For example, reading a file might throw an IO Exception, etc. In Java, the compiler won't even let you compile unless you put your code in try/catch blocks but the C# compiler doesn't seem to mind? I am particularly interested in what Exceptions are thrown by HttpWebResponse.
2
323
by: Martin Smith | last post by:
If a class has no need for any specific clean up code in a particular catch block is there any benefit to catching and rethrowing exceptions, rather than just letting the original exception bubble up to the calling function If so what?
3
3367
by: redefined.horizons | last post by:
I'm new to C#, (coming from a Java programming background), and I have a question about the correct use of exceptions. My book "Programming C#" by O'Reilly says the following in Chapter 11, "Handling Exceptions", on page 245: "An error is caused by user action. For example, the user might enter a number where a letter is expected. Once again, an error might cause an exception, but you can still prevent that by catching errors with...
5
2864
by: Steven T. Hatton | last post by:
I haven't thought about rethrowing an exception in a long time, and tried to do it the wrong way. Now I'm curious about /why/ it's wrong. My expectation was that the exception instance would simply be passed up the call stack as the same object. It would therefore behave polymorphically. Is the behavior shown below compiler-specific? I'm using gcc 4.0.2. In terms of what goes on at runtime, why does the `throw e' act as if I threw a...
15
1900
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
In my opinion rethrowing exceptions without providing anny extra information is a totall waste Examples : in my opinion wrong : A: Public sub DoSomeStuff() Try do it
4
2376
by: Coder Guy | last post by:
I am implementing an asynchronous handler (similar to Control.EndInvoke) and if the other thread throws an exception, I catch it and rethrow it on the current thread. How can I rethrow the exception on the current thread while maintaining the stack trace? The best I've come up with is encapsulating the exception as an inner exception. I would prefer to just rethrow the exception directly -- no need for an inner exception. How does...
13
1898
by: valentin tihomirov | last post by:
I do not understand something. A caught exception stack points to an argument-free 'throw' in a catch block. Meantime, it is widely known that these statements re-throw the original exceptions. I expected, therefore, that the stack points to the original 'throw e' code line.
5
2467
by: =?Utf-8?B?QmlsbHk=?= | last post by:
asp.net 2.0 vs2005 What is best practice for exception handling on a website in vs2005? I was going to catch errors in application on_error event, log them to the event log, then send users to an error page. But looking at the event log, asp.net 2.0 logs those errors anyways, with more detail than i cen get from the exception object. So is there any point logging them to the event log, is it jsut better to let .net do this and get the...
0
9636
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
10138
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...
0
9930
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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
7485
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
6724
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
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
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.