Re: exception problem

Discussion in 'Python' started by Charles Hixson, Jun 25, 2012.

  1. On 06/24/2012 11:23 PM, Andrew Berg wrote:
    > On 6/25/2012 12:27 AM, Charles Hixson wrote:
    >
    >> The documentation section covering the except statement could stand to
    >> be a *LOT* clearer. I read the sections on the except statement and
    >> exception handlers several times and couldn't figure out was the "as"
    >> argument of the except statement was for.
    >>

    > I agree that the tutorial doesn't explain the use of "as" very well, but
    > it does cover that a bare except is not normally good to use:
    > "The last except clause may omit the exception name(s), to serve as a
    > wildcard. Use this with extreme caution, since it is easy to mask a real
    > programming error in this way!"
    >
    >
    >> I still don't really know what
    >> "as" means, except that if you use it, and you print out the "target",
    >> you'll get some kind of informative message.
    >>

    > "as" lets you refer to the exception object that was caught. I find this
    > useful mainly for exceptions that have attributes (most built-in
    > exceptions don't, but many user-defined exceptions do). A full traceback
    > is much more useful for debugging than what a simple print(exc) will give.
    > There are a few different ways to get traceback information without
    > letting the exception simply propagate and terminate the program. You
    > can get some simple information from sys.exc_info() (and you can feed
    > the traceback object to a function in the traceback module), or you can
    > log it with the logging.exception() function or the exception() method
    > of a Logger from the same module. I recommend using logging. However,
    > it's generally best to just let any unexpected exceptions propagate
    > unless the program absolutely must continue, especially when debugging.
    >

    I read that that would happen, but " print (sys.exc_info()[:2]) "
    didn't even yield a blank line. It must have executed, because the
    print statement on the line before it executed, and there wasn't a loop
    or a jump (and also execution continued "normally" [the code still has
    bugs] afterward even if the finally isn't included).

    --
    Charles Hixson
    Charles Hixson, Jun 25, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jeff
    Replies:
    0
    Views:
    1,529
  2. jeff
    Replies:
    3
    Views:
    1,689
    Scott
    Jun 26, 2003
  3. Kerri
    Replies:
    2
    Views:
    13,026
    Kevin Spencer
    Oct 27, 2003
  4. Ola
    Replies:
    0
    Views:
    532
  5. Selen
    Replies:
    0
    Views:
    2,685
    Selen
    May 28, 2004
Loading...

Share This Page