I have a lot of except Exception, e statements in my code, which poses some
problems. One of the biggest is whenever I refactor even the triviallest
thing in my code.
I would like python to abort, almost as if it were a compile-time error,
whenever it cannot find a function, or if I introduced a syntax error. But,
instead, it merrily proceeds on its way.
Is there some idiom that you use in situations like these?
thanks,
josh 7 1737
On Mar 26, 12:21 pm, "Josh" <n...@nowhere.comwrote:
I have a lot of except Exception, e statements in my code, which poses some
problems. One of the biggest is whenever I refactor even the triviallest
thing in my code.
I would like python to abort, almost as if it were a compile-time error,
whenever it cannot find a function, or if I introduced a syntax error. But,
instead, it merrily proceeds on its way.
Is there some idiom that you use in situations like these?
thanks,
josh
Try sticking in an
try:
#do something
except SyntaxError, e:
print e
sys.exit(0)
except Exception, e:
print e
# You put in as many exceptions as you like.
Mike
In <46***********************@senator-bedfellow.mit.edu>, Josh wrote:
I have a lot of except Exception, e statements in my code, which poses some
problems. One of the biggest is whenever I refactor even the triviallest
thing in my code.
I would like python to abort, almost as if it were a compile-time error,
whenever it cannot find a function, or if I introduced a syntax error. But,
instead, it merrily proceeds on its way.
Is there some idiom that you use in situations like these?
Just don't use so many ``except Exception:`` constructs that obviously
swallow exceptions they shouldn't swallow.
Ciao,
Marc 'BlackJack' Rintsch
JoshI have a lot of except Exception, e statements in my code, which
Joshposes some problems. One of the biggest is whenever I refactor
Josheven the triviallest thing in my code.
JoshI would like python to abort, almost as if it were a compile-time
Josherror, whenever it cannot find a function, or if I introduced a
Joshsyntax error. But, instead, it merrily proceeds on its way.
JoshIs there some idiom that you use in situations like these?
In general, I think you should be more specific in the exceptions you
catch. For example, if you want to look up a key in a dictionary and most
of the time it's there, but every now and again you need to add it, I'd use
something like this:
try:
val = somedict[key]
except KeyError:
# need to initialize slot
somedict[key] = INITIAL_VALUE
That is, be as precise as you can in the exceptions you catch. Also, try to
keep the body of the try block as small as you can.
Skip ky******@gmail.com wrote:
On Mar 26, 12:21 pm, "Josh" <n...@nowhere.comwrote:
>I have a lot of except Exception, e statements in my code, which poses some problems. One of the biggest is whenever I refactor even the triviallest thing in my code.
I would like python to abort, almost as if it were a compile-time error, whenever it cannot find a function, or if I introduced a syntax error. But, instead, it merrily proceeds on its way.
Is there some idiom that you use in situations like these? thanks, josh
Try sticking in an
try:
#do something
except SyntaxError, e:
print e
sys.exit(0)
except Exception, e:
print e
# You put in as many exceptions as you like.
Of course the main problem with this solution is that a piece of code
will raise a syntax error when it's compiled (i.e. at module import
time, or when the main program starts up). So in the case where "#do
something" has a syntax error it won;t be trapped because the program
hasn't got to execution by the time the error is detected.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
En Mon, 26 Mar 2007 14:21:22 -0300, Josh <no***@nowhere.comescribió:
I have a lot of except Exception, e statements in my code, which poses
some
problems.
*many* problems, I'd say. Don't do that :)
One of the biggest is whenever I refactor even the triviallest
thing in my code.
I would like python to abort, almost as if it were a compile-time error,
whenever it cannot find a function, or if I introduced a syntax error.
But,
instead, it merrily proceeds on its way.
Because you have told Python to do so, using a catch-all except clause.
Try to be as specific as possible.
Syntax errors, indentation errors and such can be caught just by compiling
the module, even before you try to test it.
Is there some idiom that you use in situations like these?
Avoid bare except clauses as much as you can.
--
Gabriel Genellina
On Mar 26, 1:07 pm, Steve Holden <s...@holdenweb.comwrote:
kyoso...@gmail.com wrote:
On Mar 26, 12:21 pm, "Josh" <n...@nowhere.comwrote:
I have a lot of except Exception, e statements in my code, which poses some
problems. One of the biggest is whenever I refactor even the triviallest
thing in my code.
I would like python to abort, almost as if it were a compile-time error,
whenever it cannot find a function, or if I introduced a syntax error. But,
instead, it merrily proceeds on its way.
Is there some idiom that you use in situations like these?
thanks,
josh
Try sticking in an
try:
#do something
except SyntaxError, e:
print e
sys.exit(0)
except Exception, e:
print e
# You put in as many exceptions as you like.
Of course the main problem with this solution is that a piece of code
will raise a syntax error when it's compiled (i.e. at module import
time, or when the main program starts up). So in the case where "#do
something" has a syntax error it won;t be trapped because the program
hasn't got to execution by the time the error is detected.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
You're right, Mr. Holden. Drat! I stupidly assumed that he meant some
code executed first and then it'd hit one of his syntax errors. Either
way though, it would get "caught"...either by the interpreter (IDLE)
or the exception. At least, that's been my experience with syntax
errors.
I get a warning from IDLE that tells me there's an error at so-and-so.
Or the program just chokes and throws out and error after doing some
calculations and I learn once again that I didn't close a string.
Mike
On Mon, 26 Mar 2007 13:21:22 -0400, Josh wrote:
I have a lot of except Exception, e statements in my code, which poses some
problems. One of the biggest is whenever I refactor even the triviallest
thing in my code.
I would like python to abort, almost as if it were a compile-time error,
whenever it cannot find a function, or if I introduced a syntax error. But,
instead, it merrily proceeds on its way.
Is there some idiom that you use in situations like these?
Yes. Don't use "except Exception".
Seriously. You almost always should only catch the specific type of
exception that you expect, and treat anything else as a real error and let
it actually halt the program. e.g. instead of something like this:
try:
do_something_complicated()
except Exception, e:
# Missing key or invalid index?
print "Exception $s occurred!" % str(e)
handle_missing_key_or_index()
do this:
try:
do_something_complicated()
except KeyError:
print "Lost key"
handle_missing_key()
except IndexError:
print "Missing key"
handle_index_error()
In my opinion, the only real use for "except Exception" is something like
this:
if __name__ == "__main__"":
try:
main()
except Exception: # or just "except:"
print "A fatal error occurred, but what it is is a secret."
sys.exit(1)
finally:
cleanup()
--
Steven. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by Daniel |
last post: by
|
7 posts
views
Thread by Morris |
last post: by
|
20 posts
views
Thread by Doug Thews |
last post: by
|
5 posts
views
Thread by [Yosi] |
last post: by
|
18 posts
views
Thread by Urs Vogel |
last post: by
|
1 post
views
Thread by Hari Sekhon |
last post: by
|
5 posts
views
Thread by Alan T |
last post: by
|
6 posts
views
Thread by Marvin Barley |
last post: by
|
6 posts
views
Thread by mehdi |
last post: by
| | | | | | | | | | |