473,383 Members | 1,822 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,383 software developers and data experts.

What's the best way to wrap a whole script in try..except?

I want to wrap a whole script in try ... except. What is the best way of
doing this?

Consider the following: -

try:
import <modules>

<CODE>

def notifyme(traceback):
code to tell me there is a problem

except Exception, traceback:
notifyme(traceback)
Would this code not work because if any part of <CODE> encounters an
exception then it won't reach the notifyme() function definition and
therefore the whole thing won't work and I won't get notified when a
traceback occurs, in fact the call to notifyme() under except will
itself probably trace back as well!

Do I have to instead do:

import <a couple of modules>
def notifyme():
code to tell me there is a problem

try:
<CODE>

except Exception, traceback:
notifyme(traceback)
How you you handle this?
Hari
Jun 21 '06 #1
2 1611
In article <ma***************************************@python. org>, Hari Sekhon wrote:
I want to wrap a whole script in try ... except. What is the best way of
doing this?


You could do this maybe:

import sys

def excepthook(exc_type, exc_value, tb):
import modules_needed_to_notify_exception
...

sys.excepthook = excepthook

import modules_needed_by_script
...
Jun 21 '06 #2
Hari Sekhon wrote:
I want to wrap a whole script in try ... except. What is the best way of
doing this?

Consider the following: -

try:
import <modules>

<CODE>

def notifyme(traceback):
code to tell me there is a problem

except Exception, traceback:
notifyme(traceback)
Would this code not work because if any part of <CODE> encounters an
exception then it won't reach the notifyme() function definition and
therefore the whole thing won't work and I won't get notified when a
traceback occurs, in fact the call to notifyme() under except will
itself probably trace back as well!
Yes.
Do I have to instead do:

import <a couple of modules>
def notifyme():
code to tell me there is a problem

try:
<CODE>

except Exception, traceback:
notifyme(traceback)

Would work, but...
How you you handle this?


I don't put the main logic at the top level - I use a main() function.

import <some modules>
def notifyme(e):
# code here...

def main(*args):
try:
# code here
return 0

except Exception, e:
notifyme(e)
return <some-non-zero-error-code>

if __name__ == '__main__':
import sys
sys.exit(main(*sys.argv))

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 21 '06 #3

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

Similar topics

3
by: Pieter Van Waeyenberge | last post by:
Hello, im some image elements using doc.createElement('IMG'); i set the following styles: - for the images container: white-space:normal; - for the images themselves: display: inline; i...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
9
by: developer | last post by:
Does anyone know what is the way IE treats span tags(<span>) and table tags(<tr>, <td>)? Should the <span> tag be encolsed in tds and trs if it placed with other elements that are in a table? Can...
3
by: Razvan | last post by:
Hello, Can somebody recommend me a Java Script scroller that can scroll an i-frame ? I tried the Tigra scroller (www.softcomplex.com/products/tigra_scroller/) but sometimes it does not...
7
by: Christopher Benson-Manica | last post by:
Thanks to the fact that Safari and other browsers have decided that window.onerror is not worth supporting, I'm looking for a clean(er) way to catch errors than simply tossing try/except blocks in...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
19
by: Onideus Mad Hatter | last post by:
http://www.backwater-productions.net/_test_platform/cbstory/ Works perfect in IE but in Netscape/Firefox it fuxors. The problem is the transparent image overlays. The obvious approach would be...
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
13
by: Boris | last post by:
Can anyone tell me if Opera 9.5 is behaving correctly when wrapping the word C++, eg: C+ + Opera 9.2 didn't wrap C++. For those who use Opera 9.5 there is a test case at...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.