I dont know if that makes any sense, but basically I am automating something and I know some things will fail because, well, they don't work from time to time. I still want to check everything at regular intervals. If I use a try and except block, I can move on no prob. Problem is I can have a zillion of these kinds of methods to check, and putting them all in try/except blocks is a pain in the ass. So I want to write a function that will catch the exception and move on. Here is some simple sample code:
Now, the problem is newObj.failMe("Forced to fail.") fails before it gets to checkIt:
Is this possible, or do I have to bite the bullet and do:
each time I want to validate a method is working?
Code:
#!/usr/bin/env python
import traceback
class TestMe:
def failMe(self, str):
raise NameError(str)
# cause an exception to test checkIt func
def checkIt(objMethod):
try:
objMethod
# Normally do more, like write status for a report
except:
print traceback.format_exc()
# write fail status to a report, just printing exception in this example
newObj = TestMe()
checkIt(newObj.failMe("Forced to fail."))
print "Do more things, like more checkIt() tests"
Now, the problem is newObj.failMe("Forced to fail.") fails before it gets to checkIt:
Code:
someones-Mac-mini:~ somedude$ ./catchFail.py
Traceback (most recent call last):
File "./catchFail.py", line 21, in <module>
checkIt(newObj.failMe("Forced to fail."))
File "./catchFail.py", line 16, in failMe
raise NameError(str)
NameError: Forced to fail.
Is this possible, or do I have to bite the bullet and do:
Code:
try:
newObj.failMe("Forced to fail.")
except:
print traceback.format_exc()
each time I want to validate a method is working?