Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,128 | Comments: 45,550

filter by tags archive

Exception handling for flow control is EVIL!!!

time to read 4 min | 792 words

I just spent over half a day trying to fix a problem in NHibernate. To be short one of the updates that I made caused a backward compatibility error, and I really wanted to fix it. The actual error condition happen only when you use triple nested detached criteria with reference to the root from the inner most child. To make things fun, that change was close to 9 months ago, and over a 1,000 revisions.

I finally found the revision that introduced the error, and started looking at the changes. It was a big change, and a while ago, so it took some time. Just to give you some idea, here is where the failure happened:

public string GetEntityName(ICriteria criteria)
  ICriteriaInfoProvider result;
  if(criteriaInfoMap.TryGetValue(criteria, out result)==false)
    throw new ArgumentException("Could not find a matching criteria info provider to: " + criteria);
  return result.Name;

For some reason, the old version could find it, and the new version couldn’t. I traced how we got the values to criteriaInfoMap every each way, and I couldn’t see where the behavior was different between the two revisions.

Finally, I resorted to a line by line revert of the revision, trying to see when the test will break. Oh, I forgot to mention, here is the old version of this function:

public string GetEntityName(ICriteria criteria)
  string result;
  criteriaEntityNames.TryGetValue(criteria, out result);
  return result;

The calling method (several layers up) looks like this:

private string[] GetColumns(string propertyName, ICriteria subcriteria)
  string entName = GetEntityName(subcriteria, propertyName);
  if (entName == null)
    throw new QueryException("Could not find property " + propertyName);
  return GetPropertyMapping(entName).ToColumns(GetSQLAlias(subcriteria, propertyName), GetPropertyName(propertyName));

But even when I did a line by line change, it still kept failing. Eventually I got fed up and change the GetEntityName to return null if it doesn’t find something, instead of throw.

The test passed!

But I knew that returning null wasn’t valid, so what the hell(!) was going on?

Here is the method that calls the calling method;

public string[] GetColumnsUsingProjection(ICriteria subcriteria, string propertyName)
  // NH Different behavior: we don't use the projection alias for NH-1023
    return GetColumns(subcriteria, propertyName);
  catch (HibernateException)
    //not found in inner query , try the outer query
    if (outerQueryTranslator != null)
      return outerQueryTranslator.GetColumnsUsingProjection(subcriteria, propertyName);

I actually tried to track down who exactly wrote this mantrap, (this nasty came from the Hibernate codebase). But I got lost in migrations, reformatting, etc.

All in all, given how I feel right now, probably a good thing.



Link doesn't work :(

Link makes bunny cry.


I think I've seen that one in other projects...

Grimace of Despair

Wasn't there a time in which the only way to foolproof convert a string to an integer in .NET was to wrap the conversion in a try...catch?

Patrick Huizinga

I would like to add that the only exception (heh) I can think of is an OperationCanceledException or equivalent.

I once rewrote a few-hundred line method with "if canceled then return" all over the place (up to four levels deep) into a dozen methods with "if canceled then throw" and a catch in the entry method. Imho that made it much easier to follow.


@Grimace: Yes, int.tryParse was only introduces in .Net 2.0...

Alex Simkin

Yeah, blame everything on Hybernate

Alessandro Riolo

Such things are the reason why Ctrl+Alt+E is the most commonly pressed key combination on my keyboards :)

Asmur Trashir

And how about this, I saw this left by a previous worker of the company where I work and it was an ASP.NET application and this was broadly used:

public bool IsLeapYear(int year)


 DateTime leapYear = DateTime.MinValue;



      leapYear = new DateTime(year,2,29);




      // Year is not leap


 return (leapYear != DateTime.MinValue);



Nice to see you again talking and working on NHibernate. I think you got very busy with RabenDB. Do not forget that there are still many people who follow you with the intention to know more about your experiences and advices with NHibernate.

Steve Py

@Asmur: You've got to be kidding?! I mean, had that guy never heard of MSDN/Google/ etc?! IsLeapYear is already a method on DateTime since .Net 1.1, Anytime I come across a requirement like that that I haven't come across before, the first thing I do is search the web. More than half the time, it's already part of the framework. The remaining 49.5% of the time someone has already come up with a pretty good solution.

Heaven help any developer I catch checking in something like that. They will be bringing in donuts and/or coffee for the team for a week.

Asmur Trashir

@Steve Py: I wish I were kidding. First day in the company and I caught that.

We were having issues with web services proxies. The ASP.NET web site was instantiating correctly every web service proxy (another ASP.NET project in the same solution) but executing any method would result in exceptions.

The problem was that the programmer (yes, programmer. Programmers only type code, they dont understant what happens behind the scenes like a Developer) placed proxy service url's in VS designer sections, the ones that get deleted everytime You make a change in design. In devbox they were running fine, but at the client exceptions were thrown. Finding that resulted in finding the method I posted. Actually, I just copy-paste the original code, because I keep it as a reminder of what can go wrong when dealing with programmers instead of developers.

That same programmer made another web application that was single-user. By single-user I mean a web application that only allows a single user to be logged in and browsing the webapp. As soon as I could I rewrote it as a 3-tier layered desktop application.

What really hit me was my boss telling me "thats why we no longer hire new-grads. Thats why we are hiring now experienced people.", I almost yelled "so we are still wrong, we need people with good common sense...". Nowadays I personally interview candidates and I just ask them a few common sense questions like "I need to know if a Year is Leap, you have the next options as answers, you can select one or more or execute them in any order: google-it, search msdn library, ask for directions/tips, write some code.". If they dont discard write some code or have that as last resort I just end the interview thanking them and black-listing them.

Comment preview

Comments have been closed on this topic.


  1. The worker pattern - about one day from now

There are posts all the way to May 30, 2016


  1. The design of RavenDB 4.0 (14):
    26 May 2016 - The client side
  2. RavenDB 3.5 whirl wind tour (14):
    25 May 2016 - Got anything to declare, ya smuggler?
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats