Created attachment 25766 [details] Unit tests illustrating the problems Two problems with exception thrown by ProxyDirContext.lookup - ProxyDirContext.lookup throws notFoundException when the entry doesn't exist. notFoundException is a field and therefore initialized at ProxyDirContext creation. The consequence is that the exception thrown contains a stacktrace that has nothing to do with the current execution stack. This makes difficult to find the root cause of a problem. If this is really an optimization (what is the gain), then what about making this field static? - ProxyDirContext.lookup is not consistent: for the same call it may throw NamingException or NameNotFoundException depending whether the cache has been used or not. Problems occur in Tomcat 6 and 7.
The gain by using notFoundException is in GC. It prevents exception objects being created for resources that don't exist. I agree it could be static (final too).
Thanks for the test case. I kept the "not Found" test (with a minor modification" but not the strack trace test. The returned exceptions have been made consistent for 7.0.x and will be included in 7.0.1 onwards.
Can you explain why GC gain are considered at this special place in detriment of a correct exception? For me if GC gain is an argument, then there are so many other places in Tomcat where it should apply. This starts with the BaseDirContext.lookup that you've fixed to address the "not Found" point of this case.
No, I didn't write the original code but I trust that Remy had a good reason for this particular optimisation.