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

Returning Error Codes vs. Throwing Exceptions

I've been struggling with this question for a while. What is better
design? To design functions to return error codes when an error
occures, or to have them throw exceptions.

If you chose the former, i have a few questions that need to be
answered.

1) What about functions that need to return a value regardless of the
error. How can they also return an error code unless the function has
"output" parameters. This seems messy and ridiculous.

2) What if the direct caller is not interested in the error. We will
have some kind of error code chain going on. This seems also messy.
If you chose the latter.
1) What about performance?
2) Is there no problem in having too many exceptions, which can cause
the function call to exit from too many places?

I'm sure there are questions concerning these issues that I have not
asked. If you can pose them and answer them I would be greatful.

Thanks a lot

Mark
Jul 21 '05 #1
5 4654
Mark Oueis <ma*******@hotmail.com> wrote:
I've been struggling with this question for a while. What is better
design? To design functions to return error codes when an error
occures, or to have them throw exceptions.
Generally, throw exceptions if the method can't fulfil what it's meant
to do. In some cases, that will be so common that it should be part of
what it's meant to do, if you see what I mean. For instance, a
TextReader coming to the end isn't unusual, so ReadLine returns null
rather than throwing an exception.

<snip>
If you chose the latter.
1) What about performance?
Exceptions are almost entirely free if they're not thrown. If they
*are* thrown, it's certainly more expensive than a return code which is
examined - but you'll have saved time in all the other code where you'd
have been examining "no error" return codes. Exceptions aren't the
performance-killers they're made out to be, unless they're thrown
ridiculously often (like in a tight loop).
2) Is there no problem in having too many exceptions, which can cause
the function call to exit from too many places?


I don't personally view that as a problem, but I've never been a fan of
the "only return from one place" idea anyway. I find it far easier to
read code which says "exit the method now, in this way" than code which
sets up elaborate flow control solely for the sake of only exiting the
method in one place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
I remember Joel Spolsky getting into this debate long back against
exceptions.

--
Sriram Krishnan

http://www.dotnetjunkies.com/weblog/sriram
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mark Oueis <ma*******@hotmail.com> wrote:
I've been struggling with this question for a while. What is better
design? To design functions to return error codes when an error
occures, or to have them throw exceptions.


Generally, throw exceptions if the method can't fulfil what it's meant
to do. In some cases, that will be so common that it should be part of
what it's meant to do, if you see what I mean. For instance, a
TextReader coming to the end isn't unusual, so ReadLine returns null
rather than throwing an exception.

<snip>
If you chose the latter.
1) What about performance?


Exceptions are almost entirely free if they're not thrown. If they
*are* thrown, it's certainly more expensive than a return code which is
examined - but you'll have saved time in all the other code where you'd
have been examining "no error" return codes. Exceptions aren't the
performance-killers they're made out to be, unless they're thrown
ridiculously often (like in a tight loop).
2) Is there no problem in having too many exceptions, which can cause
the function call to exit from too many places?


I don't personally view that as a problem, but I've never been a fan of
the "only return from one place" idea anyway. I find it far easier to
read code which says "exit the method now, in this way" than code which
sets up elaborate flow control solely for the sake of only exiting the
method in one place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
Sriram Krishnan <ks*****@NOSPAMgmx.net> wrote:
I remember Joel Spolsky getting into this debate long back against
exceptions.


Yes - I read his article very recently. I disagree with him entirely,
and would be interested to see his code - it must either be incredibly
hard to read (with an if block round every significant line of code) or
fail to detect some potential errors.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Hi Mark,

first of all, there is a difference between Errorrs and exceptions

Errors occurs due to user mistake. Though the errors can also led an
exception but a programmer should handle this thing through its validation
code.
Exception should only be used for known but preventable situation like out
of memory etc.. Though Microsoft has put his sincere effort in creating
exception classes for every type of exception including that occurs through
error but a good programmer should avoid using these things. Apart from that
the exception handling in itself is a memory intensive and should be avoided
through validation code and other checks .Depending on the requirement you
have to make the additional stuff to minimize the errors

I hope it solves your problem

regards
Nishith

"Mark Oueis" wrote:
I've been struggling with this question for a while. What is better
design? To design functions to return error codes when an error
occures, or to have them throw exceptions.

If you chose the former, i have a few questions that need to be
answered.

1) What about functions that need to return a value regardless of the
error. How can they also return an error code unless the function has
"output" parameters. This seems messy and ridiculous.

2) What if the direct caller is not interested in the error. We will
have some kind of error code chain going on. This seems also messy.
If you chose the latter.
1) What about performance?
2) Is there no problem in having too many exceptions, which can cause
the function call to exit from too many places?

I'm sure there are questions concerning these issues that I have not
asked. If you can pose them and answer them I would be greatful.

Thanks a lot

Mark

Jul 21 '05 #5
Thanks, I think that makes sence. I was leaning towards exception
throwing. It seems much cleaner.

Another thing that has to do with Exceptions. I heard that there is
problem throwing exceptions from within a constructor of a class in
C++. Is this valid in .NET? I heard something along the lines of "half
created object" and "possible memory leaks" for c++. Is there any such
problem in .NET or can I just throw as many exceptions as I want in
the contructor.

Mark
Jul 21 '05 #6

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

Similar topics

9
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
8
by: Shane Groff | last post by:
I know this is a recurring discussion (I've spent the last 3 days reading through threads on the topic), but I feel compelled to start it up again. After reading through the existing threads, I...
2
by: Paul Reddin | last post by:
Hi, I've implemented calling a SP form a trigger using the CALL_PROCEDURE() UDF from http://www-106.ibm.com/developerworks/db2/library/techarticle/dm-0402greenstein/ A couple of questions...
5
by: Mark Oueis | last post by:
I've been struggling with this question for a while. What is better design? To design functions to return error codes when an error occures, or to have them throw exceptions. If you chose the...
21
by: Jim Langston | last post by:
I'm sure this has been asked a few times, but I'm still not sure. I want to create a function to simplify getting a reference to a CMap in a map. This is what I do now in code: ...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
25
by: Noah Roberts | last post by:
It is my understanding that this behavior has long been deprecated and is not standard. That is the creation of an array of objects using new returning 0 instead of throwing an exception. I'm not...
13
by: mike3 | last post by:
Hi. (crossposted because the program is in C++ and some C++-related elements are discussed, hence comp.lang.c++, plus general program design questions are asked, hence comp.programming.) I'm...
16
by: john6630 | last post by:
Coming from the .Net world, I am used to the try...catch...finally approach to error handling. And PHP 5 now supports this approach. But I am not clear what happens to unhandled errors/exceptioins?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
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.