how to abort on syntax errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Josh

    #1

    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


  • kyosohma@gmail.com

    #2
    Re: how to abort on syntax errors

    On Mar 26, 12:21 pm, "Josh" <n...@nowhere.c omwrote:
    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

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: how to abort on syntax errors

      In <46080186$0$141 51$b45e6eb0@sen ator-bedfellow.mit.e du>, 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

      Comment

      • skip@pobox.com

        #4
        Re: how to abort on syntax errors


        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

        Comment

        • Steve Holden

          #5
          Re: how to abort on syntax errors

          kyosohma@gmail. com wrote:
          On Mar 26, 12:21 pm, "Josh" <n...@nowhere.c omwrote:
          >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

          Comment

          • Gabriel Genellina

            #6
            Re: how to abort on syntax errors

            En Mon, 26 Mar 2007 14:21:22 -0300, Josh <noone@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

            Comment

            • kyosohma@gmail.com

              #7
              Re: how to abort on syntax errors

              On Mar 26, 1:07 pm, Steve Holden <s...@holdenweb .comwrote:
              kyoso...@gmail. com wrote:
              On Mar 26, 12:21 pm, "Josh" <n...@nowhere.c omwrote:
              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"...eith er 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

              Comment

              • Steven D'Aprano

                #8
                Re: how to abort on syntax errors

                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_co mplicated()
                except Exception, e:
                # Missing key or invalid index?
                print "Exception $s occurred!" % str(e)
                handle_missing_ key_or_index()


                do this:

                try:
                do_something_co mplicated()
                except KeyError:
                print "Lost key"
                handle_missing_ key()
                except IndexError:
                print "Missing key"
                handle_index_er ror()




                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.

                Comment

                Working...