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

To throw or to throw not?

I'm pondering on what is a bit of a philosophical dilemma.
When should I throw an exception and when should I not?

Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3
().
Suppose myFunc3() has detected a problem. What should it do?

Throw an exception, forcing myFunc2() to handle it and/or trigger
another exception for myFunc1() to deal with? Or should it simply
return a meaningful error code, for myFunc2() and myFunc1() to handle
as an option but not forcing them to do so?

Manu
Nov 14 '08 #1
6 1496
On Thu, Nov 13, 2008 at 5:11 PM, Emanuele D'Arrigo <ma****@gmail.comwrote:
I'm pondering on what is a bit of a philosophical dilemma.
When should I throw an exception and when should I not?

Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3
().
Suppose myFunc3() has detected a problem. What should it do?

Throw an exception, forcing myFunc2() to handle it and/or trigger
another exception for myFunc1() to deal with? Or should it simply
return a meaningful error code, for myFunc2() and myFunc1() to handle
as an option but not forcing them to do so?
Depends on how serious the error is (e.g. str.find() returns -1 rather
than raising an exception if it can't find the substring), but 98% of
the time, you'll want to raise an exception; it's Pythonic, idiomatic,
and expected. You'd have to have a *really* good reason to use an
error value/code instead.
Python is not C, and despite what Joel has said On Software, error
codes generally suck.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Manu
--
http://mail.python.org/mailman/listinfo/python-list
Nov 14 '08 #2
On Nov 13, 7:11*pm, "Emanuele D'Arrigo" <man...@gmail.comwrote:
I'm pondering on what is a bit of a philosophical dilemma.
When should I throw an exception and when should I not?

Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3
().
Suppose myFunc3() has detected a problem. What should it do?

Throw an exception, forcing myFunc2() to handle it and/or trigger
another exception for myFunc1() to deal with? Or should it simply
return a meaningful error code, for myFunc2() and myFunc1() to handle
as an option but not forcing them to do so?
That depends on the situation, doesn't it?

For example, if I want to solve a linear congruence

X*a == Z (mod Y)

all I have to do is check that GCD(X,Y) divides Z and
I can go ahead and call the solving function...

....which requires use of the modular inverse function
which raises an exception if GCD(X,Y) is not 1 (even if
it does divide Z).

But wait! If, in fact, the GCD(X,Y)>1, and I already know
that GCD(X,Y) divides Z, then it divides X, Y and Z, so
I just divide each by GCD(X,Y) to make a new linear
congruence where the modular inverse function will work
and I'll get the right answer.

The answer is that IF the exception can be handled without
the calling function needing to know, then just handle it.
Otherwise pass it back in case the calling function can
figure out what to do.
>
Manu
Nov 14 '08 #3
Emanuele D'Arrigo wrote:
I'm pondering on what is a bit of a philosophical dilemma.
When should I throw an exception and when should I not?

Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3
().
Suppose myFunc3() has detected a problem. What should it do?

Throw an exception, forcing myFunc2() to handle it and/or trigger
another exception for myFunc1() to deal with? Or should it simply
return a meaningful error code, for myFunc2() and myFunc1() to handle
as an option but not forcing them to do so?
Remember that with exceptions, if Func2 doesn't want to process the
exception it doesn't have to do anything at all to have the exception
re-raised: it simply doesn't trap the exception.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 14 '08 #4
On Thu, 13 Nov 2008 17:11:26 -0800, Emanuele D'Arrigo wrote:
I'm pondering on what is a bit of a philosophical dilemma. When should I
throw an exception and when should I not?

Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3
().
Suppose myFunc3() has detected a problem. What should it do?

Throw an exception, forcing myFunc2() to handle it and/or trigger
another exception for myFunc1() to deal with? Or should it simply return
a meaningful error code, for myFunc2() and myFunc1() to handle as an
option but not forcing them to do so?
In general, you should raise an exception. Then if myFunc2() can't handle
it, it doesn't need to trigger another exception, the exception will just
propagate up the call chain to myFunc1().

There are cases where something like a meaningful error value is
appropriate, but the only one I can think of right now is NaN in floating
point maths.
--
Steven
Nov 14 '08 #5
On Nov 13, 10:09*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Thu, 13 Nov 2008 17:11:26 -0800, Emanuele D'Arrigo wrote:
I'm pondering on what is a bit of a philosophical dilemma. When should I
throw an exception and when should I not?
Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3
().
Suppose myFunc3() has detected a problem. What should it do?
Throw an exception, forcing myFunc2() to handle it and/or trigger
another exception for myFunc1() to deal with? Or should it simply return
a meaningful error code, for myFunc2() and myFunc1() to handle as an
option but not forcing them to do so?

In general, you should raise an exception. Then if myFunc2() can't handle
it, it doesn't need to trigger another exception, the exception will just
propagate up the call chain to myFunc1().

There are cases where something like a meaningful error value is
appropriate, but the only one I can think of right now is NaN in floating
point maths.

--
Steven
If you need error flags, define them in a class:

class ErrFlags:
onlyInCorner= object( )
livesOnCeiling= object( )

def myFunc3( ):
something( )
return ErrFlags.onlyInCorner

def myFunc2( ):
result= myFunc3( )
if result is ErrFlags.onlyInCorder:
something
elif result is ErrFlags.livesOnCeiling:
something

In some cases, you might want to call a method on the return object.

I agree with the fellows earlier. Without further information,
exceptions are generally nice and solid.
Nov 14 '08 #6
Thank you all for the insightful replies! Much appreciated!

Manu
Nov 15 '08 #7

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

Similar topics

8
by: Christian Schuhegger | last post by:
hi, we are working on a c++ project which has a lot of thow list specifications which in the end causes a lot of problems. now i would like to remove them from the project. because the project...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
1
by: Rennie deGraaf | last post by:
I want to use throw() specifiers on some of my methods while debugging, to help me keep track of what is going on, but remove them in release builds. My method of doing this is #ifdef DEBUG...
17
by: hplloyd | last post by:
Hi, I have a function that adds data to a database and I want to return true if the database was updated and false if there was a problem, so I am using a try... catch block... My problem is...
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
1
by: aemado | last post by:
I am trying to read in several lines, each should have exactly 5 pieces of data. I am using try/catch/throw to determine if the data is in the correct format, and trying to use iss to separate the...
4
by: Samant.Trupti | last post by:
Hi, bool CControlDrvInstance::IsRebootRequired() const throw() In this code what is throw() do? I just have slight knowledge that is a exception handler but how does it work. If I have this in...
6
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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,...

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.