[prog] Java exceptions: finding out the source

Rachel McConnell rachel at xtreme.com
Tue Jun 29 05:24:11 EST 2004


Mary wrote:

> On Tue, Jun 29, 2004, Mary wrote:
> 
>>Somewhere in the code, this exception is being produced:
>>
>>    Exception in thread "main"
> 
> 
> I ended up finding out (from the project mailing list) that this error
> happens when you don't put a certain .jar file in the classpath.
> 
> I'd still be interested in people's thoughts on what makes Java produce
> such a useless error message...

I'm a bit surprised that you didn't get additional info:

Exception in thread "main" java.lang.NoClassDefFoundError: ClassName

specifically noting which class wasn't found, if the problem was a 
missing jar.  Care to provide more detail?

If I were having this problem & it wasn't a missing jar, I'd probably 
try for a bit more information by using something like,

public void main(String[] args) {
   try {

     // existing code here

   } catch( Exception e ) {
     e.printStackTrace();
   }
}

This basically catches ANY exception in the whole application, since it 
wraps the starting point, and provides at least a stack trace on it. 
(You can pass an output stream to printStackTrace to tell it where to 
write to; default is unsurprisingly Standard Out.)

As to why that message sucks, well partly it's the programmer's 
responsibility to either extend Exception with a more specific class, 
pass in a more detailed error message to the exception's constructor, or 
both (my preference).  For a missing class, this is not the kind of 
thing the application is likely to handle as it's really a deployment 
issue rather than an application issue.

And partly, the base class Exception is way too general to be useful, 
and gets used anyway.  Arguably it ought to have been abstract.

I am more peeved, personally, with the output of a NullPointerException, 
which, it seems to me, really OUGHT to tell you what is null!

Rachel




More information about the Programming mailing list