Android try catch all exceptions
In Exception-based code antipatterns Max Ischenko argues this type of error checking isn’t good:
# make a move
except Exception, e:
# handle it
It’s basically equal to using except: without indicating any exception class whatsoever, meaning that it’ll catch all exceptions. Generally it’s easier to only catch the exceptions you’re expecting, e.g., KeyError or (IOError, OSError). I do not disagree with him, but you will find numerous cases when this type of exception handling is nice, though frequently omitted.
Probably the most apparent situation: it’s pretty good whatsoever to catch all exceptions should you re-bring them up. In Python you are able to re-enhance the last exception by utilizing raise with no arguments. Essentially you’re saying if (and only when) something goes completely wrong, do that, but nonetheless signal that something went wrong. This situation should not be mistaken with finally:, which signifies code that ought to always be known as, error or no error. A good example of except::
cursor.execute(“Place INTO table VALUES (%s, %s)”, (a, b))
And something for finally::
conn = pscyopg.connect(‘dbname=testdb’)
There is however other cases. When you will not be there to babysit a procedure, you have to handle unpredicted exceptions. For any command-line utility, read the exception if this happens, no large deal. However for a lengthy-running or batch process you have to smartly cope with exceptions. But in addition processes which are run by non-designers: you need to capture the mistake information so a developer can view it, after which try to help keep going if you’re able to.
In these instances you typically desire to be careful about investing in except: blocks such as the cursor.rollback() example above — these make an effort to clean some misconception when something unpredicted goes completely wrong, to ensure that the procedure is not left within an sporadic condition. Then at some fairly higher level you really handle (and do not re-raise) the exception. Most “frameworks” do that for you personally — e.g., Tkinter, Webware, Zope, cgitb, etc. Without having a framework, you need to do-it-yourself. Here is a rather easy handler that utilizes the traceback module:
def catch_exceptions(func, *args, **kw):
# We’re able to return caused by the function, but it is
# an awful idea to take advantage of this return value becasue it is
# undefined when we have an exception it’s better for that
# function to create a flag or email some persistent
# structure if this finishes effectively.
except Exception, e:
f = open(log_file, ‘a’)
f.write(‘-’*60 + ‘n’)
The logging module also offers the best function due to this situation.
I am always careful about situations in which a traceback or error message would go to a black hole. Also, it’s annoying for everybody when these error messages need to be handed to designers from customers expecting a person to repeat and email a mistake message from the web site is hard to rely on throughout. Lastly, it’s annoying to get rid of the effect of a lengthy process since you encounter a mistake. That emerged lately having a colliege as he attempted to make use of HTMLParser to spider webpages — regrettably HTMLParser does not work nicely on badly-created HTML (I’d advise using BeautifulSoup, regular expressions, or running the code through tidy first). Therefore the process would run for some time, then fail and die if this found a singular type of malformed HTML. This
may be the type of unpredicted exception that needs to be expected, and you ought to write your program knowing you have not foreseen all of the problems you’ll encounter.
(BTW, I have wanted an excellent generic library to deal with unpredicted exceptions, but nonetheless haven’t thought it was… anybody are conscious of one?)