how to abort on syntax errors

J

Josh

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
 
K

kyosohma

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
 
M

Marc 'BlackJack' Rintsch

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
 
S

skip

Josh> I have a lot of except Exception, e statements in my code, which
Josh> poses some problems. One of the biggest is whenever I refactor
Josh> even the triviallest thing in my code.

Josh> I would like python to abort, almost as if it were a compile-time
Josh> error, whenever it cannot find a function, or if I introduced a
Josh> syntax error. But, instead, it merrily proceeds on its way.

Josh> Is 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
 
S

Steve Holden

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
 
G

Gabriel Genellina

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.
 
K

kyosohma

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
 
S

Steven D'Aprano

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()
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top