Help needed: try/except -idiom vs. C-style coding

P

pekka niiranen

Hi there,

I have defined function that calls another functions like this
(modified for this mail):

def main_function():
if not function1():
m = "main_function: function1 failed"
print_error(m)
return False
if not function2():
m = "main_function: function2 failed"
print_error(m)
return False
if not function3():
m = "main_function: function3 failed"
print_error(m)
return False
return True

In book "Learning Python 1st edition" is was said
that this C-style programming is bad practice
and should be replaced with structure:

def main_function():
try:
function1():
function2():
function3():
except:
what_ever()
return False (?)
else:
return True (?)

How can I use try/except -style if I want to know WHICH
of functions 1-3 failed in try: -section and print
that information? What should functions return in order to
a) raise an exception and
b) tell main_function "it was me who failed for this reason"
This message should be user specific.

Note that main_function might also be subroutine and must
information of its failure to its upper level.

I am looking something like:

def main_function():
try:
function1():
function2():
function3():
except: !!!! FIX !!!!!
print "Function %s failed in main_function" % functions_name print
"Reason: %s" % functions_error_message

Does try/except -idiom expect that message
'"print "Reason: %s" % functions_error_message'
is always printed at the function itself, not by
its caller's except : -section?

-pekka-
 
D

Duncan Booth

pekka said:
How can I use try/except -style if I want to know WHICH
of functions 1-3 failed in try: -section and print
that information? What should functions return in order to
a) raise an exception and
b) tell main_function "it was me who failed for this reason"
This message should be user specific.

If you feel you *must* print the name of the function that failed then you
can extract it from the traceback object:
try:
function1()
function2()
function3()
except RuntimeError:
type, value, tb = sys.exc_info()
filename, lineno, functionname, text = traceback.extract_tb(tb)[-1]
print functionname

function2

A more usual way to handle this would be to just print a traceback. That
way the user gets not only the information about the function that threw
the exception, but also the route that led to that function.
try:
function1()
function2()
function3()
except RuntimeError:
traceback.print_exc()
Traceback (most recent call last):
File "<pyshell#50>", line 4, in main
File "<pyshell#44>", line 1, in function2
RuntimeError: oops
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top