#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Python try/catch problem


    Hello,

    I'm using

    try:
    myfunction()
    except Exception:
    print "failed"

    in a loop however if the myfunction causes a problem it just loops through the rest of the loop printing "failed".

    Is it possible to have a try/catch scenario where if the function call fails for whatever reason it can just continue the loop?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    383
    Rep Power
    0
    First of all: If you want to catch any exception, rather use "except:" instead of "except Exception:" (which just catches the exception called "Exception").

    I guess the whole try-except is inside the loop? If yes, chances are that the function just fails and fails and fails...
    To avoid multiple "failed" outputs, put the try above the loop:
    Code:
    try:
        for i in xrange(10): # just "range" if 3.x
            myFunction()
    except:
        print 'failed'
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0
    Hello,

    Thanks for the tip, I'm using the plain exception: and so far no issues
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by delnan
    First of all: If you want to catch any exception, rather use "except:" instead of "except Exception:" (which just catches the exception called "Exception").
    ]
    this is not true. except Exception: will catch anything derived from the Exception class. Since this is the root class of all exceptions then it should catch all exceptions. (I say "should" since it is possible to raise exceptions that are not derived from it, but anyone that does that is stupid).

    Dave
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    383
    Rep Power
    0
    "except SomeException" also catches Exceptions derived from SomeException? Never heard about this, but great if it works!
    But I still wouldn't focus on catching Exception - as you said, if someone is stupid enough, his/her exception may be not derived from Exception... but knowing that this person is stupid won't make the except clause work
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by delnan
    "except SomeException" also catches Exceptions derived from SomeException? Never heard about this, but great if it works!
    Yes it does work. That is why there is a heriarchy of exceptions. You catching an exception somewhere in the heirarchy will catch an exceptions derived from it. This is also true of most languages that have exceptions, including C++, Java and C#.

    But I still wouldn't focus on catching Exception - as you said, if someone is stupid enough, his/her exception may be not derived from Exception... but knowing that this person is stupid won't make the except clause work
    I think that from Python 2.6 the compiler enforces that all exceptions must be derived from the Exception heirarchy.

    Also by catching a specific exception type you can also capture the exception object itself in a variable and get additional information from it. You can also do different things depending on the type of exception thrown:

    python Code:
    try:
        doStuff()
    except IOError:
        print "failed to open file"
    except MemoryError:
        print "out of memory"
    except Exception, e:
        print "Unknown exception", e


    Dave
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    383
    Rep Power
    0
    You catching an exception somewhere in the heirarchy will catch an exceptions derived from it. This is also true of most languages that have exceptions, including C++, Java and C#.
    I think that from Python 2.6 the compiler enforces that all exceptions must be derived from the Exception heirarchy.
    Good to know, thanks for the explanation!

    Also by catching a specific exception type you can also capture the exception object itself in a variable and get additional information from it. You can also do different things depending on the type of exception thrown:
    I'm sure there are many things I don't know, but I can assure that I already knew this Thanks again, nevertheless.

IMN logo majestic logo threadwatch logo seochat tools logo