Community
Participate
Working Groups
Eclipse 3.2 I'm trying to debug a ResourceException somewhere in my code so I've added a exception breakpoint for ResourceException. The problem is that I get tons of hits in JavaProject.hasJavaNature because it is implemented the following way: public static boolean hasJavaNature(IProject project) { try { return project.hasNature(JavaCore.NATURE_ID); } catch (CoreException e) { if (ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(project.getName())) return true; // project does not exist or is not open } return false; } Instead of checking if the project.exists() or project.isOpen() it relies on an exception being thrown... Which makes it painful to everyone else trying to debug a ResourceException.
The same happens in JavaProject.exists(): public boolean exists() { try { return this.project.hasNature(JavaCore.NATURE_ID); } catch (CoreException e) { // project does not exist or is not open } return false; }
There is no guaranty that a CoreException will not be thrown even if we check that the project exists() and isOpen(). So we still need to catch the CoreException. Also catching an exception is much faster than testing exists() and isOpen(). If you don't want to see the exception thrown in this code, you can still configure the debugger to not stop on this code (see the Filtering section of the breakpoint properties). Will not change this.
Not sure if I agree with it being much faster than checking for open or exists. In this case I get lots of exceptions which I believe are not cheap to handle.
Verified for 3.4M4
Exception handling approach is only faster if the exception never gets thrown (i.e. avoiding an eager check 99.99% of the time). Rodrigo seems to imply that this is not what is happening...
Rodrigo, what is your use case? Do you have many projects closed in your workspace? If so how many? Looking at the code of Project.isOpen(), it looks like very expensive (many messages are sent in this code). I still believe that catching the exception is faster. I will need to write a performance test to verify this.
Hey guys, I can't find the workspace anymore. It was happening when I was importing an existing workspace with many Java projects. If I find it I can reopen this bug.