Catch exceptions

Discussion in 'Python' started by Cruella DeVille, Feb 27, 2006.

  1. In my application of bookmarks I get a filename as a parameter in one
    of my methods. I want to catch the "FileNotFoundException" if the user
    types an invalid filename. My code (that doesn't do what I want it to
    do - namely print a nicely formatted error message, and no stack trace)

    def openFile(self):
    try:
    return open(self.fileName, self.mode)
    except IOError:
    print("%s" %("The file was not found"))

    But this one never occurs even if I write the name of a non-existing
    file.

    I also assign a variable, filePointer to a filename (the result of
    openFile(self)), with a AttributeError
    if self.filePointer is None:
    raise AttributeError, "Assignment failed"

    But I still get this stack trace (or trackback?)

    How do I implement a try- except? Is it in my method where i open the
    file or when I try to open the file in my "main-method"?
    Cruella DeVille, Feb 27, 2006
    #1
    1. Advertising

  2. Cruella DeVille

    Crutcher Guest

    Without seeing more of your code, I'm not sure what you are doing
    wrong. This works:
    vvvvvvvv ex.py vvvvvvvvvvvvvv
    def xopen(path):
    try:
    return open(path)
    except IOError, e:
    print 'Error:', e.args[1]

    xopen('xyzzy')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    $ python ex.py
    Error: No such file or directory

    Could you give us the traceback?
    Crutcher, Feb 28, 2006
    #2
    1. Advertising

  3. I put the try catch in my main-method, and it worked to some extent
    (except from when I try to read from a file with no content)

    Any idea how to solve that?
    And is it a good place to have a try-except in main instead of in the
    class methods?

    My code is at http://nibbler.no/blog/wp-includes/Bookmarks.py
    Cruella DeVille, Feb 28, 2006
    #3
  4. (A) You'll have to be more specific about your problem if you want us
    to help. Under which circumstances did it work or not? What exactly is
    the problem?

    (B) Where you put your error-handling is up to you. It depends on what
    the function does and how it is to respond. Do you want the function to
    throw an exception if the file is not found, or do you want it to do
    something else? Think about the interface of the functions (which is
    the sum of the parameters, response, possible exceptions, and expected
    behavior) and it should be clear where to put the exception handling.
    Jonathan Gardner, Feb 28, 2006
    #4
    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. Alan Silver

    How do I catch unhandled exceptions

    Alan Silver, Mar 20, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    1,728
    Alan Silver
    Mar 23, 2006
  2. Chris Berg

    How to catch multiple Exceptions

    Chris Berg, Jan 19, 2004, in forum: Java
    Replies:
    8
    Views:
    15,703
    Chris Berg
    Jan 19, 2004
  3. John Black
    Replies:
    8
    Views:
    4,153
    Xenos
    Aug 20, 2004
  4. Adam
    Replies:
    9
    Views:
    573
    red floyd
    Feb 2, 2006
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    565
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page