exceptions

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

    exceptions


    Hi,

    I've the following problem with try/exception.
    I've a try block that will raise some exceptions.
    I want the program to ignore this exceptions completely.
    Is it possible?

    Thanks in advance

    Zunbeltz

    --
    Zunbeltz Izaola Azkona | wmbizazz at lg dot ehu
    dotes
    Materia Kondentsatuaren Fisika Saila |
    Zientzia eta Teknologia Fakultatea | Phone: 34946015326
    Euskal Herriko Unibertsitatea |
    PK 644 | Fax: 34 944648500
    48080 Bilbo (SPAIN) |
  • Peter Hansen

    #2
    Re: exceptions

    Zunbeltz Izaola wrote:[color=blue]
    > I've the following problem with try/exception.
    > I've a try block that will raise some exceptions.
    > I want the program to ignore this exceptions completely.
    > Is it possible?[/color]

    In the following, all exceptions will be ignored:

    try:
    # something that raises exceptions
    except:
    pass

    This is definitely *not* a recommended way to write software,
    however. You should really understand what you are doing before
    writing code this way.

    -Peter

    Comment

    • Brian Gough

      #3
      Re: exceptions

      Zunbeltz Izaola <zunbeltz@wm.lc .ehu.es.XXX> writes:
      [color=blue]
      > I've the following problem with try/exception.
      > I've a try block that will raise some exceptions.
      > I want the program to ignore this exceptions completely.
      > Is it possible?[/color]

      You can use an empty "except:" to catch all exceptions and ignore them,

      try:
      # your code
      except:
      pass

      See the Python tutorial or language reference manual for details.

      --
      Brian Gough

      Network Theory Ltd,
      Publishing the Python Manuals --- http://www.network-theory.co.uk/

      Comment

      • Zunbeltz Izaola

        #4
        Re: exceptions

        Peter Hansen <peter@engcorp. com> writes:
        [color=blue]
        > Zunbeltz Izaola wrote:[color=green]
        > > I've the following problem with try/exception.
        > > I've a try block that will raise some exceptions.
        > > I want the program to ignore this exceptions completely. Is it
        > > possible?[/color]
        >
        > In the following, all exceptions will be ignored:
        >
        > try:
        > # something that raises exceptions
        > except:
        > pass
        >[/color]

        I've trid this, but the problem is that this finished the try block.
        I want to continue executing the try block from the point the
        exception was raised.
        [color=blue]
        > This is definitely *not* a recommended way to write software,
        > however. You should really understand what you are doing before
        > writing code this way.
        >[/color]

        I know. I need this feature for testing. I will desable it later.

        Zunbeltz
        [color=blue]
        > -Peter[/color]

        --
        Zunbeltz Izaola Azkona | wmbizazz at lg dot ehu
        dotes
        Materia Kondentsatuaren Fisika Saila |
        Zientzia eta Teknologia Fakultatea | Phone: 34946015326
        Euskal Herriko Unibertsitatea |
        PK 644 | Fax: 34 944648500
        48080 Bilbo (SPAIN) |

        Comment

        • Jeff Epler

          #5
          Re: exceptions

          Use "pass" as the body of the "except:" block. For example:

          def put(s, i, j):
          """Store j at s[i], or do nothing if s is not that long"""
          try:
          s[i] = j
          except IndexError:
          pass
          [color=blue][color=green][color=darkred]
          >>> l = [1, 2, 3]
          >>> put(l, 1, "hi")
          >>> put(l, 4, "bye")
          >>> l[/color][/color][/color]
          [1, 'hi', 3]

          Jeff

          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.2.4 (GNU/Linux)

          iD8DBQFAuyCpJd0 1MZaTXX0RAgFJAJ 4sSYM9XKWuzJk9v zXGMgnU2EhfZgCf R70/
          YjRBsk9yMptdSNW Podl46vQ=
          =hQG9
          -----END PGP SIGNATURE-----

          Comment

          • Peter Hansen

            #6
            Re: exceptions

            Zunbeltz Izaola wrote:[color=blue]
            > Peter Hansen <peter@engcorp. com> writes:[color=green]
            >>Zunbeltz Izaola wrote:
            >>[color=darkred]
            >>>I've the following problem with try/exception.
            >>>I've a try block that will raise some exceptions.
            >>>I want the program to ignore this exceptions completely. Is it
            >>>possible?[/color]
            >>
            >>In the following, all exceptions will be ignored:
            >>
            >>try:
            >> # something that raises exceptions
            >>except:
            >> pass[/color]
            >
            > I've trid this, but the problem is that this finished the try block.
            > I want to continue executing the try block from the point the
            > exception was raised.[/color]

            Not possible except, perhaps, with a significant amount of
            working involving sys.settrace(). I think you need to find
            another way to solve the problem.

            What you are trying to do is not something that other people
            who do unit testing seem to have to do. Why do you think
            you need to actually *disable* exceptions entirely in order
            to test this code? Perhaps the code needs to be restructured?

            -Peter

            Comment

            • Zunbeltz Izaola

              #7
              Re: exceptions

              Peter Hansen <peter@engcorp. com> writes:
              [color=blue]
              > Zunbeltz Izaola wrote:[color=green]
              > > Peter Hansen <peter@engcorp. com> writes:[color=darkred]
              > >>Zunbeltz Izaola wrote:
              > >>
              > >>>I've the following problem with try/exception.
              > >>>I've a try block that will raise some exceptions.
              > >>>I want the program to ignore this exceptions completely. Is it
              > >>>possible?
              > >>
              > >>In the following, all exceptions will be ignored:
              > >>
              > >>try:
              > >> # something that raises exceptions
              > >>except:
              > >> pass[/color]
              > > I've trid this, but the problem is that this finished the try block.
              > > I want to continue executing the try block from the point the
              > > exception was raised.[/color]
              >
              > Not possible except, perhaps, with a significant amount of
              > working involving sys.settrace(). I think you need to find
              > another way to solve the problem.
              >
              > What you are trying to do is not something that other people
              > who do unit testing seem to have to do. Why do you think
              > you need to actually *disable* exceptions entirely in order
              > to test this code? Perhaps the code needs to be restructured?[/color]

              Possibly the code should be restructered, and re-designed; there is
              always room for imporovement. But what I'm doing is not unittest. My
              program is controling and instrument (an x-ray powder
              diffractometer) and some parts of the instrument are not working for
              the moment, so i want to disable all error i get from this instrument
              (are coded like exceptions)

              Zunbeltz[color=blue]
              >
              > -Peter[/color]

              --
              Zunbeltz Izaola Azkona | wmbizazz at lg dot ehu
              dotes
              Materia Kondentsatuaren Fisika Saila |
              Zientzia eta Teknologia Fakultatea | Phone: 34946015326
              Euskal Herriko Unibertsitatea |
              PK 644 | Fax: 34 944648500
              48080 Bilbo (SPAIN) |

              Comment

              • Irmen de Jong

                #8
                Re: exceptions

                Zunbeltz Izaola wrote:
                [color=blue]
                > Possibly the code should be restructered, and re-designed; there is
                > always room for imporovement. But what I'm doing is not unittest. My
                > program is controling and instrument (an x-ray powder
                > diffractometer) and some parts of the instrument are not working for
                > the moment, so i want to disable all error i get from this instrument
                > (are coded like exceptions)[/color]

                What I usually do in comparable situations is to write STUB code for
                the parts of the system that don't work yet.
                Write your stub code so that it does nothing, but doesn't raise any
                exceptions too. The only thing you then have to do is write the rest
                of the code as you would have done, and once the Stubbed parts work,
                replace the stub code with the real code.

                --Irmen

                Comment

                • Calvin Spealman

                  #9
                  Re: exceptions

                  Peter Hansen wrote:[color=blue]
                  > Zunbeltz Izaola wrote:[color=green]
                  >> I've trid this, but the problem is that this finished the try block.
                  >> I want to continue executing the try block from the point the
                  >> exception was raised.[/color]
                  >
                  > Not possible except, perhaps, with a significant amount of
                  > working involving sys.settrace(). I think you need to find
                  > another way to solve the problem.[/color]

                  Have to admit tho, a continue feature might be useful. Some languages have
                  this, don't they? The thing is, Where exactly to continue? Should you retry
                  whatever raised the exception, continue just after it, at the beginning of
                  that line, or what?


                  Comment

                  • Scott David Daniels

                    #10
                    Re: exceptions

                    Calvin Spealman wrote:[color=blue]
                    > ...
                    > Have to admit tho, a continue feature might be useful. Some languages have
                    > this, don't they? The thing is, Where exactly to continue? Should you retry
                    > whatever raised the exception, continue just after it, at the beginning of
                    > that line, or what?
                    >
                    >[/color]
                    See this older thread:
                    <http://groups.google.c om/groups?threadm= 3edd6118%241%40 nntp0.pdx.net>

                    Xerox's experience (in deliberately removing the "continue from
                    exception" language feature) I found very instructive.

                    -Scott David Daniels
                    Scott.Daniels@A cm.Org

                    Comment

                    • John J. Lee

                      #11
                      Re: exceptions

                      Irmen de Jong <irmen@-nospam-remove-this-xs4all.nl> writes:
                      [color=blue]
                      > Zunbeltz Izaola wrote:
                      >[color=green]
                      > > Possibly the code should be restructered, and re-designed; there is
                      > > always room for imporovement. But what I'm doing is not unittest. My
                      > > program is controling and instrument (an x-ray powder
                      > > diffractometer) and some parts of the instrument are not working for
                      > > the moment, so i want to disable all error i get from this instrument
                      > > (are coded like exceptions)[/color]
                      >
                      > What I usually do in comparable situations is to write STUB code for
                      > the parts of the system that don't work yet.
                      > Write your stub code so that it does nothing, but doesn't raise any
                      > exceptions too. The only thing you then have to do is write the rest
                      > of the code as you would have done, and once the Stubbed parts work,
                      > replace the stub code with the real code.[/color]

                      ....and if you think you want to get exceptions later:

                      def fixme():
                      pass


                      Sprinkle fixme()s through your code, then redefine later:

                      def fixme():
                      raise NotImplementedE rror()


                      John

                      Comment

                      • Peter Hansen

                        #12
                        Re: exceptions

                        Zunbeltz Izaola wrote:
                        [color=blue]
                        > Peter Hansen <peter@engcorp. com> writes:[color=green]
                        >>Zunbeltz Izaola wrote:[color=darkred]
                        >>>I want to continue executing the try block from the point the
                        >>>exception was raised.[/color]
                        >>
                        >>Not possible except, perhaps, with a significant amount of
                        >>working involving sys.settrace(). I think you need to find
                        >>another way to solve the problem.[/color]
                        >
                        > But what I'm doing is not unittest.[/color]

                        Pardon: I don't know why I thought this was related to testing
                        code.
                        [color=blue]
                        > My program is controling and instrument (an x-ray powder
                        > diffractometer) and some parts of the instrument are not working for
                        > the moment, so i want to disable all error i get from this instrument
                        > (are coded like exceptions)[/color]

                        What is the interface to the instrument? Is there some sort of
                        driver/wrapper layer that handles the communication with the
                        device? Your only hope, I think, is to intercept things at
                        that level and avoid/trap the exceptions before they get up
                        to the higher level. In effect, stubs as Irmen suggested...

                        It sounds like you are quite capable of figuring it out at
                        this point, though, since all you wanted to know was whether
                        you could continue after an exception and now you know you
                        cannot. :-)

                        -Peter

                        Comment

                        • Ng Pheng Siong

                          #13
                          Re: exceptions

                          According to Scott David Daniels <Scott.Daniels@ Acm.Org>:[color=blue]
                          > Calvin Spealman wrote:[color=green]
                          > > ...
                          > > Have to admit tho, a continue feature might be useful. Some languages have
                          > > this, don't they? The thing is, Where exactly to continue? Should you retry
                          > > whatever raised the exception, continue just after it, at the beginning of
                          > > that line, or what?
                          > >[/color]
                          > See this older thread:
                          > <http://groups.google.c om/groups?threadm= 3edd6118%241%40 nntp0.pdx.net>
                          >
                          > Xerox's experience (in deliberately removing the "continue from
                          > exception" language feature) I found very instructive.[/color]

                          Are the bugs mainly in the implementation of that feature, or in user
                          code attempting to use said feature?

                          (I notice the older thread was one year ago. Is this an annual topic? ;-)

                          BTW, Common Lisp has this, called "restarts". There are about half a dozen
                          Common Lisp implementations and none appear to have gotten restarts
                          terribly wrong.

                          Cheers.


                          --
                          Ng Pheng Siong <ngps@netmemeti c.com>

                          http://firewall.rulemaker.net -+- Firewall Change Management & Version Control
                          http://sandbox.rulemaker.net/ngps -+- ZServerSSL/Zope Windows Installers

                          Comment

                          • Jeff Epler

                            #14
                            Re: exceptions

                            > According to Scott David Daniels <Scott.Daniels@ Acm.Org>:[color=blue][color=green]
                            > > See this older thread:
                            > > <http://groups.google.c om/groups?threadm= 3edd6118%241%40 nntp0.pdx.net>[/color][/color]

                            On Tue, Jun 01, 2004 at 12:59:29AM +0000, Ng Pheng Siong wrote:[color=blue]
                            > (I notice the older thread was one year ago. Is this an annual topic? ;-)[/color]

                            Perhaps the older thread had an exception, but now it's been re-started?

                            Jeff

                            -----BEGIN PGP SIGNATURE-----
                            Version: GnuPG v1.2.4 (GNU/Linux)

                            iD8DBQFAu/DWJd01MZaTXX0RA jtjAJ9U2x7VZ4Et 2LwlH7B4mFK7gXQ yEwCeKwlA
                            l1ze/lr+g0ena9p+Sq87 e9I=
                            =PqxJ
                            -----END PGP SIGNATURE-----

                            Comment

                            • Jacek Generowicz

                              #15
                              Re: exceptions

                              Scott David Daniels <Scott.Daniels@ Acm.Org> writes:
                              [color=blue]
                              > Calvin Spealman wrote:[color=green]
                              > > ...
                              > > Have to admit tho, a continue feature might be useful. Some languages have
                              > > this, don't they? The thing is, Where exactly to continue? Should you retry
                              > > whatever raised the exception, continue just after it, at the beginning of
                              > > that line, or what?
                              > >[/color]
                              >
                              > See this older thread:
                              > <http://groups.google.c om/groups?threadm= 3edd6118%241%40 nntp0.pdx.net>
                              >
                              > Xerox's experience (in deliberately removing the "continue from
                              > exception" language feature) I found very instructive.[/color]

                              Funny, Common Lisp (and some of its ancestors) allows you to "fiddle
                              things and and return as well as even return from the exception", and
                              people who write industrial strength applications is Common Lisp
                              repeatedly state that this feature allows them to write software which
                              is much more robust than any they would otherwise be able to
                              create. Some go as far as considering any language without this
                              feature to be fundamentally flawed. I have never heard anyone identify
                              this feature as a source of bugs.

                              So I am tempted to conclude that the Mesa implementation was flawed
                              .... or maybe it was just the users who were flawed. The concept itself
                              seems to have proven its worth over time.

                              Comment

                              Working...