By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

how to abort on syntax errors

P: n/a
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
Mar 26 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Mar 26 '07 #2

P: n/a
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
Mar 26 '07 #3

P: n/a

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
Mar 26 '07 #4

P: n/a
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

Mar 26 '07 #5

P: n/a
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

Mar 26 '07 #6

P: n/a
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

Mar 26 '07 #7

P: n/a
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.

Mar 26 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.