exception handling

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

    exception handling

    Hello,


    def error_msg(msg):
    sys.exit(msg)

    try:
    do_something()
    if value != my_wish:
    error_msg('Inva lid input')
    except:
    print "Fatal IO or Network error"

    This doesn't work because sys.exit raises an exception.

    I know that I can define exception types after except, but there might
    be many. Also I know I can write:
    except:
    if str(sys.exc_inf o()[0]) == 'exceptions.Sys temExit':
    raise

    But honestly I would like simething like that:

    except (!SystemExit):

    Is this possible somehow?

    Mage
  • Peter Otten

    #2
    Re: exception handling

    Mage wrote:
    [color=blue]
    > def error_msg(msg):
    > sys.exit(msg)
    >
    > try:
    > do_something()
    > if value != my_wish:
    > error_msg('Inva lid input')
    > except:
    > print "Fatal IO or Network error"
    >
    > This doesn't work because sys.exit raises an exception.
    >
    > I know that I can define exception types after except, but there might
    > be many. Also I know I can write:
    > except:
    > if str(sys.exc_inf o()[0]) == 'exceptions.Sys temExit':
    > raise
    >
    > But honestly I would like simething like that:
    >
    > except (!SystemExit):
    >
    > Is this possible somehow?[/color]

    try:
    # may raise any exception
    except SystemExit:
    raise # propagate SystemExit (and subclasses)
    except:
    # handle everything else

    Peter

    Comment

    • Roy Smith

      #3
      Re: exception handling

      Mage <mage@mage.hu > wrote:
      [color=blue]
      > Hello,
      >
      >
      > def error_msg(msg):
      > sys.exit(msg)
      >
      > try:
      > do_something()
      > if value != my_wish:
      > error_msg('Inva lid input')
      > except:
      > print "Fatal IO or Network error"
      >
      > This doesn't work because sys.exit raises an exception.[/color]

      Peter Otten posted a good way to do what you want, but I'm not convinced
      that it's a good idea.

      You're assuming that any exception you get will be a "Fatal IO or Network
      error". What if it's not? What if do_something() raises FutureWarning, or
      KeyboardInterru pt, or AssertionError, or DeprecationWarn ing, or anything
      else which has nothing to do with IO or networks? You would just end up
      producing a misleading error message.

      If you expect do_something() will throw certain exceptions, catch them
      specifically. It's usually a mistake to catch everything. It's certainly
      a mistake to catch everything and assume you know what it must be.

      Try running the following code and see what happens:

      import traceback

      def do_something():
      pass

      try:
      do_something()
      if value != my_wish:
      error_msg('Inva lid input')
      except:
      print "Fatal IO or Network error"
      print traceback.print _exc()

      Comment

      Working...