Language Wish-List: Add Information to Exceptions

Following a discussion over on David MacIver's blog. I have a piece of functionality I'd like to see in more languages.

The ability to easily add information to exceptions

try {
  // ...
} catch (Exception e) {
  e.addInformation("serverName", serverName);
  throw e;

Or, even better, a way to annotate variables to get this for free/cheap:

@PutInExceptions String serverName = config.getServerName;

The advantage of being able to add this information is that it makes it easier to diagnose exceptions without losing the type of the exception. Often, when we see an exception we get a full stack trace telling us the line that caused it but not enough surrounding information to make replication easy. Consider the following:

public User getUserDetails(int id) {
  if (!cache.contains(User.class, id)) {
    // Populate the cache
  User user = cache.get(User.class, id);
  log("Got user with name ["+user.getName()+"]");
  return user;
If this blows up on the log line then all we know is that user is null. We don't know the id that was used. This can make debugging harder.

Having the ability to annotate things as "add this to any exception that occurs" makes it easy and cheap to get good error handling. If you then want a method to be ultra-fast then you don't add the annotations - simple.


blog comments powered by Disqus

Colin Howe

I'm Colin. I like coding, ultimate frisbee and startups. I am VP of engineering at Conversocial