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

how to abort on syntax errors

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
7 1818
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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
7
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
5
by: [Yosi] | last post by:
Why I can't abot a susspended thread. Who can terminat a thread imediatly without consider to its stat or execution?
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
1
by: Hari Sekhon | last post by:
I've written an except hook into a script as shown below which works well for the most part and catches exceptions. import sys def myexcepthook(type,value,tb): do something ...
5
by: Alan T | last post by:
I will do several things in my thread: Copy a file to a location Update database record Read the file content Write the content to a log file If I call Thread.Abort(), it may be possible to...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.