Javascript Exception Handling With the Visitor Pattern

I recently help a friend fix some convoluted javascript exception handling code, and I thought that the javascript community might find it useful.

The Problem

Javascript does not allow multiple catch clauses based on exception type. In programming languages like Java, we are allowed to write code like this

However, javascript only allows a single catch clause; so, exception handling code is often written like the following

The above code leaves much to be desired. This is what I wanted to improve

The Solution

We can use the visitor pattern to dynamically choose the right algorithm based on the events type.

This refactor makes the code easier to read and maintain.

For example, in the previous implementation each if statement would increase the cyclomatic complexity by 1. The refactor has made the cyclomatic complexity of the catch clause 1 for any number of exceptions.


The solutions we created needs to be supported by code specifically designed to facilitate the visitor pattern.

As a first step, we we create a set of modules that inherit from the built-in Error object. The following is an example of how we implement the inheritance mechanism using native javascript:

Note: This is just one way to implement inheritance in javascript. If the following code looks unfamiliar to you then I suggest that you spend some time studying the internals of how javascript supports polymorphism.

The function addExceptionHandlingVisitor acts as a decorator for our custom Errors. It’s purpose is to allow each error module to visit an error handler. It does this by attaching a visit function to any module that is passed to it.

In the code above, The visit function expect an exception handling module (i.e. context). The context variable is expected to have two function: test and handle. The test function is expected to inform the visitor whether or not it is a handler for the error, and the handle function is expected to actually handle the error.

Consider the following implementations of handlers for DuplicateUsernameError and CreditCardRejectedError.

Note: We could refactor this to have an inheritance hierarchy with reusable code, but I don’t believe that their would be much benefit in going to that effort.

Complete Code

A working example is also located on my jsfiddle account:


I am not making any claims that this is the best way to handle exception handling in javascript. However, you should not allow any piece of your code be written with complicated if-else statements.

If you find yourself writing a lot of conditional logic in single place then that should set an alarm in your head that reminds you to simplify your code with the use of polymorphism. Simplifying complicated conditional logic like nested if-else statements is exactly why polymorphism was invented.

Just remember that javascript is not java, c++, or c#. javascript is javascript. It has it’s own form of polymorphism, and you should know how to use it properly.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s