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):
    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

    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. Advertisements

  2. Crutcher

    Crutcher Guest

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

    $ python
    Error: No such file or directory

    Could you give us the traceback?
    Crutcher, Feb 28, 2006
    1. Advertisements

  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
    Cruella DeVille, Feb 28, 2006
  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
    1. Advertisements

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
    Alan Silver
    Mar 23, 2006
  2. Chris Berg

    How to catch multiple Exceptions

    Chris Berg, Jan 19, 2004, in forum: Java
    Chris Berg
    Jan 19, 2004
  3. John Black
    Aug 20, 2004
  4. Adam
    red floyd
    Feb 2, 2006
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Daniel Pitts
    Aug 5, 2009

Share This Page