Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way of handling exceptions in Python?

I have searched for other posts, as I felt this is a rather common problem, but all other Python exception questions I have found didn't reflect my problem.

I will try to be as specific here as I can, so I will give a direct example. And pleeeeease do not post any workarounds for this specific problem. I am not specifically interested how you can send an email much nicer with xyz. I want to know how you generally deal with dependent, error prone statements.

My question is, how to handle exceptions nicely, ones that depend on one another, meaning: Only if the first step was successful, try the next, and so on. One more criterion is: All exceptions have to be caught, this code has to be robust.

For your consideration, an example:

try:     server = smtplib.SMTP(host) #can throw an exception except smtplib.socket.gaierror:     #actually it can throw a lot more, this is just an example     pass else: #only if no exception was thrown we may continue     try:         server.login(username, password)     except SMTPAuthenticationError:         pass # do some stuff here     finally:         #we can only run this when the first try...except was successful         #else this throws an exception itself!         server.quit()      else:         try:             # this is already the 3rd nested try...except             # for such a simple procedure! horrible             server.sendmail(addr, [to], msg.as_string())             return True         except Exception:             return False         finally:             server.quit()  return False 

This looks extremely unpythonic to me, and the error handling code is triple the real business code, but on the other hand how can I handle several statements that are dependent on one another, meaning statement1 is prerequisite for statement2 and so on?

I am also interested in proper resource cleanup, even Python can manage that for itself.

Thanks, Tom

like image 488
Tom Avatar asked Jun 12 '09 00:06

Tom


People also ask

How many ways can you handle exceptions in Python?

When a Python code throws an exception, it has two options: handle the exception immediately or stop and quit.


1 Answers

Instead of using the try/except's else block, you could simply return when it errors:

def send_message(addr, to, msg):     ## Connect to host     try:         server = smtplib.SMTP(host) #can throw an exception     except smtplib.socket.gaierror:         return False      ## Login     try:         server.login(username, password)     except SMTPAuthenticationError:         server.quit()         return False      ## Send message     try:         server.sendmail(addr, [to], msg.as_string())         return True     except Exception: # try to avoid catching Exception unless you have too         return False     finally:         server.quit() 

That's perfectly readable and Pythonic..

Another way of doing this is, rather than worry about the specific implementation, decide how you want your code to look, for example..

sender = MyMailer("username", "password") # the except SocketError/AuthError could go here try:     sender.message("addr..", ["to.."], "message...") except SocketError:     print "Couldn't connect to server" except AuthError:     print "Invalid username and/or password!" else:     print "Message sent!" 

Then write the code for the message() method, catching any errors you expect, and raising your own custom one, and handle that where it's relevant. Your class may look something like..

class ConnectionError(Exception): pass class AuthError(Exception): pass class SendError(Exception): pass  class MyMailer:     def __init__(self, host, username, password):         self.host = host         self.username = username         self.password = password      def connect(self):         try:             self.server = smtp.SMTP(self.host)         except smtplib.socket.gaierror:             raise ConnectionError("Error connecting to %s" % (self.host))      def auth(self):         try:             self.server.login(self.username, self.password)         except SMTPAuthenticationError:             raise AuthError("Invalid username (%s) and/or password" % (self.username))      def message(self, addr, to, msg):         try:             server.sendmail(addr, [to], msg.as_string())         except smtplib.something.senderror, errormsg:             raise SendError("Couldn't send message: %s" % (errormsg))         except smtp.socket.timeout:             raise ConnectionError("Socket error while sending message") 
like image 179
dbr Avatar answered Sep 22 '22 03:09

dbr