Pass An Error Handler

last modified: July 23, 2013

Instead of letting code throw exceptions around, pass an error handler to it that it can consult in the event of a failure.

This is a simple form of the StrategyPattern.

ExceptionHandlingChallenge entry:

Following the example in ExceptionHandlingChallenge, Python code might be:

def handle_database_failure():
    redirect("databasefailure") # assuming this jumps somewhere (bad idea, but same as in the example)

def handle_email_failure():
         UserDatabase.delete(email, handle_database_failure)
         redirect("mailfailure")

User'Database.add(user, handle_database_failure)
mailPassword(email, password, handle_email_failure)
redirect("success")    

Note how the exception that could arise from the UserDatabase.delete is handled properly. You might say "well, we'd just spot that and add another try/catch block in there". However, the point is that when passing an error handler, the handling is enforced by the delete method as it requires the handler in a parameter. I.e. if it doesn't get one, we find out straight away that the code is broken.

Pros:

Cons:

Further notes:


Cf. ResumableException, CommonLispConditionSystem


CategoryException


Loading...