473,549 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ 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.Resul ts.OK Then

' We failed.

End If

, where "DoSomethin g" 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 1028
Robin,

"Robin Tucker" <id************ *************@r eallyidont.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.joelonsoftw are.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
2845
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 return code (not generating error/exception) so it is more compatible with other programming language.
7
5975
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2740
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 exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you...
3
1903
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 to interface with third-party libraries, get extra performance, etc. I have to use C++. Now in Python, being a dynamically typed language,...
44
4164
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box,...
4
1841
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 it has now, after 15k lines of code resulted in a royal mess! It's my hope to ask some specific questions with scenario examples and that some of...
5
1911
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 studied Computer Science so I like to try and 'catch up' on stuff like this at the weekend. Having said that I have a couple of questions around the...
21
1544
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 basic outline of the program done which is: def Xref(filename): try: fp = open(filename, "r") lines = fp.readlines() fp.close()
1
3094
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
7450
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
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. ...
1
7470
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...
0
7809
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...
0
5088
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...
0
3500
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...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
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
0
763
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.