473,484 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Questions about exception handling..............

I keep reading about exception handling being preferable to "return codes".
I would like to implement some kind of consistent use of exceptions in my
code, but am finding it hard to come up with some definitive rules.

For example, I Try...Catch....Finally, whenever I am either dealing with an
interop object or something that needs to be "disposed" after use. This I
think is a fairly standard rule. However, I'm not "rethrowing" after
dealing with release of resources, rather - I'm returning an error object
(DataError), that can encapsulate the exception. So, the caller logic for
executing a function looks like this:

theError = DoSomething ();
If Not theError.Result = DataError.Results.OK Then

' We failed.

End If

, where "DoSomething" will return <> OK, if it catches and handles an
exception, usually a meaningful error code (similar to HRESULTS I guess).

The confusion for me arises when I read that you should let the exception
propagate to higher and higher levels, while I am tending to deal with the
exception at the lowest possible level and propagate return codes back up
instead. So the question is, is it better to rethrow, or to return an error
code? If one is preferable to the other, why? And won't constantly
rethrowing back up a hierarchy give me performance problems?
Nov 21 '05 #1
1 1020
Robin,

"Robin Tucker" <id*************************@reallyidont.com> schrieb:
The confusion for me arises when I read that you should let the exception
propagate to higher and higher levels, while I am tending to deal with the
exception at the lowest possible level and propagate return codes back up
instead. So the question is, is it better to rethrow, or to return an
error code? If one is preferable to the other, why? And won't constantly
rethrowing back up a hierarchy give me performance problems?


I think it's better to propagate the exception. This will prevent the user
(client) of the class from missing to check return values. However,
exceptions are not always the best solution and there are people who are not
as euphoric about today's use of structured exception handling.

Exceptions
<URL:http://www.joelonsoftware.com/items/2003/10/13.html>

Note that you can add information to an exception by creating a new
exception object and adding the existing exception as inner exception. Then
you simply need to throw the new exception.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

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

Similar topics

11
2828
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
7
5959
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2734
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
3
1899
by: Gonçalo Rodrigues | last post by:
Hi all, I've got two somewhat general questions related to exception handling in C++. My usual programming language is the lovely and flexible Python, but for various reasons, including the need...
44
4155
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
1837
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
5
1903
by: DC | last post by:
Hi all, First of all, let me asure you you will not be doing my homework for me if you are good enough to reply - I have been a professional programmer for a couple of years now, I just never...
21
1541
by: nateastle | last post by:
I have a simple assignment for school but am unsure where to go. The assignment is to read in a text file, split out the words and say which line each word appears in alphabetical order. I have the...
1
3086
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
7103
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
7137
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...
1
6809
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
7194
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
5403
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
3044
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...
0
1355
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 ...
1
587
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.