printing error message from an Exception

Discussion in 'Python' started by mark jason, Dec 10, 2010.

  1. mark jason

    mark jason Guest

    hi
    I was trying out some file operations and was trying to open a non
    existing file as below

    def do_work(filename):
    try:
    f = open(filename,"r");
    print 'opened'
    except IOError, e:
    print 'failed',e.message
    finally:
    f.close()
    print 'closed'

    if __name__=='__main__':
    do_work("C:\code\misc.txt") # there is no such file

    I am getting an error and a warning

    DeprecationWarning: BaseException.message has been deprecated as of
    Python 2.6
    print 'failed',e.message

    UnboundLocalError: local variable 'f' referenced before assignment

    Is there a way to overcome the DeprecationWarning? I wanted to print
    the error message from the IOError.How do I do this?
    Also ,what should I do about the UnboundLocalError?

    thanks and regards ,
    mark
     
    mark jason, Dec 10, 2010
    #1
    1. Advertising

  2. On Thu, 09 Dec 2010 22:16:17 -0800, mark jason wrote:

    > hi
    > I was trying out some file operations and was trying to open a non
    > existing file as below
    >
    > def do_work(filename):
    > try:
    > f = open(filename,"r");
    > print 'opened'
    > except IOError, e:
    > print 'failed',e.message
    > finally:
    > f.close()
    > print 'closed'



    The problem is, you try to close a file that not only hasn't been opened,
    but the name f isn't bound to a value:

    >>> do_work('no such file')

    failed
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 8, in do_work
    UnboundLocalError: local variable 'f' referenced before assignment



    > if __name__=='__main__':
    > do_work("C:\code\misc.txt") # there is no such file
    >
    > I am getting an error and a warning
    >
    > DeprecationWarning: BaseException.message has been deprecated as of
    > Python 2.6
    > print 'failed',e.message
    >
    > UnboundLocalError: local variable 'f' referenced before assignment
    >
    > Is there a way to overcome the DeprecationWarning? I wanted to print the
    > error message from the IOError.How do I do this?


    Generally, the best way to print error messages is to print the *entire*
    error message, including the traceback. You do that by just not catching
    the exception at all.

    But for the times you do need to do something else with the exception,
    instead of exception.message, look at exception.args. This will be a
    tuple of one or more items, the first item should be the error message.


    > Also ,what should I do about the UnboundLocalError?


    Use finally for things which must be done regardless of whether or not
    the previous operation succeeded, not for things that should only be done
    if the previous operation succeeded but not if it failed.

    def do_work(filename):
    try:
    f = open(filename,"r");
    print 'opened'
    except IOError, e:
    # By the way, IOError is not the only exception you could see.
    print 'failed', e.args
    else:
    f.close()
    print 'closed'


    Or, slightly more useful:

    def do_work(filename):
    s = ''
    try:
    f = open(filename,"r");
    print 'opened'
    except IOError, e:
    # By the way, IOError is not the only exception you could see.
    print 'failed', e.args
    else:
    try:
    s = f.read()
    finally:
    f.close()
    print 'closed'
    print s



    --
    Steven
     
    Steven D'Aprano, Dec 10, 2010
    #2
    1. Advertising

  3. mark jason

    mark jason Guest

    On Dec 10, 11:55 am, Steven D'Aprano <steve
    > wrote:
    >     # By the way, IOError is not the only exception you could see.



    thanks for the help Steven. Is it OK to catch Exception instead of
    IOError ?
    In some operation which can cause many errors ,can I use the
    following?

    try:
    do_something()
    except Exception,e:
    display_message(e.args)
    handle_exception(e)


    thanks,
    mark
     
    mark jason, Dec 10, 2010
    #3
  4. mark jason

    Peter Otten Guest

    mark jason wrote:

    > hi
    > I was trying out some file operations and was trying to open a non
    > existing file as below
    >
    > def do_work(filename):
    > try:
    > f = open(filename,"r");
    > print 'opened'
    > except IOError, e:
    > print 'failed',e.message
    > finally:
    > f.close()
    > print 'closed'
    >
    > if __name__=='__main__':
    > do_work("C:\code\misc.txt") # there is no such file
    >
    > I am getting an error and a warning
    >
    > DeprecationWarning: BaseException.message has been deprecated as of
    > Python 2.6
    > print 'failed',e.message
    >
    > UnboundLocalError: local variable 'f' referenced before assignment
    >
    > Is there a way to overcome the DeprecationWarning? I wanted to print
    > the error message from the IOError.How do I do this?
    > Also ,what should I do about the UnboundLocalError?


    You are mixing two things here:

    (1) print an error message when the file cannot be opened
    (2) ensure that the file will always be closed

    If you attack both aspects separately they become simpler:

    # 1
    try:
    f = open(...)
    except IOError as e:
    print "Failed", e

    # 2a
    f = open(...)
    try:
    print "opened"
    finally:
    f.close()

    which can be simplified to

    # 2b
    with open(...) as f:
    print "opened"

    Putting 1 and 2b together:

    try:
    with open(...) as f:
    print "opened"
    except IOError as e:
    print "Failed", e

    There is a disadvantage though: an IOError that is raised in the with-suite
    will also be caught. If you're concerned about that you can use:

    try:
    f = open(...)
    except IOError as e:
    print "Failed", e
    else:
    with contextlib.closing(f):
    print "opened"
     
    Peter Otten, Dec 10, 2010
    #4
  5. mark jason wrote:
    > On Dec 10, 11:55 am, Steven D'Aprano <steve
    > > wrote:
    >
    >> # By the way, IOError is not the only exception you could see.
    >>

    >
    >
    > thanks for the help Steven. Is it OK to catch Exception instead of
    > IOError ?
    > In some operation which can cause many errors ,can I use the
    > following?
    >
    > try:
    > do_something()
    > except Exception,e:
    > display_message(e.args)
    > handle_exception(e)
    >
    >
    > thanks,
    > mark
    >

    You shouldn't.

    If you want to handle an exception, you need to know about it. What if
    your handle_exception meets some exception it does not know how to handle ?

    People are sometimes using bare try except clause to say 'look ! my
    function never crash !'. True but it usually has an unpredictable
    behavior upon exception being raised, and that is much worse that a
    clean uncought exception.


    What you can do :

    try:
    do_something()
    except IOError:
    # handler IOerror
    except KeyboardInterrupt:
    print 'stopping...'
    sys.exit(0)
    except NotImplementedError:
    # handler missing implementation

    Everything else should be kept uncought.

    JM
     
    Jean-Michel Pichavant, Dec 10, 2010
    #5
    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. =?Utf-8?B?UmlwdWwgSGFuZGE=?=

    Base Exception:System.OutOfMemoryException error message

    =?Utf-8?B?UmlwdWwgSGFuZGE=?=, Nov 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,023
    Natty Gur
    Nov 18, 2003
  2. tshad
    Replies:
    0
    Views:
    815
    tshad
    Jan 27, 2005
  3. Replies:
    2
    Views:
    583
    sloan
    Aug 18, 2006
  4. Hertha Steck
    Replies:
    0
    Views:
    283
    Hertha Steck
    Oct 9, 2006
  5. Colin Graham

    Error message when printing from HTA

    Colin Graham, Dec 15, 2004, in forum: Javascript
    Replies:
    0
    Views:
    106
    Colin Graham
    Dec 15, 2004
Loading...

Share This Page