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

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

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-
Jul 18 '05 #1
1 1096
pekka niiranen wrote:
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:
import sys, traceback
def function1(): pass def function2(): raise RuntimeError('oops') def function3(): pass def main(): try:
function1()
function2()
function3()
except RuntimeError:
type, value, tb = sys.exc_info()
filename, lineno, functionname, text = traceback.extract_tb(tb)[-1]
print functionname

main() 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.
def main(): try:
function1()
function2()
function3()
except RuntimeError:
traceback.print_exc()
main()

Traceback (most recent call last):
File "<pyshell#50>", line 4, in main
File "<pyshell#44>", line 1, in function2
RuntimeError: oops
Jul 18 '05 #2

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

Similar topics

6
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I...
6
by: Jack Smith | last post by:
Help needed on this question. Any help is appreciated. Thanks in advance. Given a binary string (i.e. a finite sequence of 0's and 1's) we choose any two digit substring 01 and replace it by a...
1
by: worzel | last post by:
Hi All, I am looking for a reg ex that will match email addresses withing <a href=mailto blah > links. Actually, I already crafted my own, but with a slight problem: <a...
2
by: Willie | last post by:
I try to setup SQL Server to work with .NET Framework Anyone here willing to help? Step By Step Help Needed. Thanks
14
by: tbird2340 | last post by:
I want to write an if / then statement and have tried using this: var MyVarMailto; if (Request.Form("LoanRequest") == "Under $250,000") { if (Request.Form("Organization") == "1") { MyVarMailto...
1
by: BHPexpert | last post by:
Regular Expression help needed -------------------------------------------------------------------------------- I want to extract all text that is contained inside the brackets after the word...
2
by: ananth | last post by:
Hi All, I have written a code that finds default mail client and then i am using a function to open a mail client and populating it with values.The code works fine when i give some minimum...
0
by: Christopher | last post by:
Urgent Help Needed: The EPVH-1.1 Visual Hull Library. Dear All, I am a student doing research in computer vision. The EPVH-1.1 Visual Hull Library will really help a lot in my research. I...
1
by: Joel Fireman | last post by:
Help Needed: Upgrade Fedora 4 / Apache 2 to PHP 5.2.x from 5.0.4 I've been testing Joomla as a content manager for the County offices, and it looks pretty good. Unfortunately, I decided to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.