Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

catch java.lang.NoClassDefFoundError

3 views
Skip to first unread message

mark...@my-deja.com

unread,
Dec 5, 2000, 3:00:00 AM12/5/00
to
I tried to load classes with forName but some classes I tried to
loaded didn't have a default constructor. That caused a
java.lang.NoClassDefFoundError which can't be cought.

How can I get around this so that I don't load what I can not load?

Thanks


Sent via Deja.com http://www.deja.com/
Before you buy.

Jon Skeet

unread,
Dec 6, 2000, 3:00:00 AM12/6/00
to
mark...@my-deja.com wrote:
> I tried to load classes with forName but some classes I tried to
> loaded didn't have a default constructor. That caused a
> java.lang.NoClassDefFoundError which can't be cought.
>
> How can I get around this so that I don't load what I can not load?

Same way you deal with any exception - use try and catch.

--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet

mark...@my-deja.com

unread,
Dec 6, 2000, 3:00:00 AM12/6/00
to
In article <MPG.149821f7...@10.1.1.51>,

Are you kiding: I don't think that you can catch run-time error.

Mark

Jon Skeet

unread,
Dec 7, 2000, 3:00:00 AM12/7/00
to
mark...@my-deja.com wrote:
> > Same way you deal with any exception - use try and catch.

> Are you kiding: I don't think that you can catch run-time error.

No, I'm not kidding - you can catch them perfectly easily.

Try this, for example:

public class Test
{
public static void main (String [] args)
{
String s = null;
try
{
s.length();
System.out.println ("Strange...");
}
catch (NullPointerException e)
{
System.out.println ("Look, caught a runtime exception");

Jon Skeet

unread,
Dec 7, 2000, 3:00:00 AM12/7/00
to
ju...@cs.purdue.edu wrote:
> What you caugtht in the example is "Exception", not an "Error".

True...

> But "Error" can be caught, but it should not be caught
> by application,, because something really weird happened.

Not necessarily. There are all kinds of cases where you can predict an
Error *may* occur - NoClassDefFoundError being one of them. Before now
I've caught NoSuchMethodException on the grounds that I can then write a
compatibility class to cope with different versions of APIs.

Karl Schmidt

unread,
Dec 7, 2000, 3:00:00 AM12/7/00
to
Kyungkoo Jun schrieb:
>
> I guess "run-time error" IN THE APPLICATION
> can be caught,, if you read "catch" statement spec,,
> it can catch any type of "Throwable" or subclasses,,
> (Error is a subclass of Throwable)
> but generally it should not be caught by application because
> something really weird happened.
>
> If I'm wrong, please correct me.

Let's say, you work with reflection. Is a NoClassDefFoundError something that
weird? Catch it at the right place and pretent nothing had happened at all :-)

It depends on what you are doing. If you know what you are doing it is ok to
catch a RuntimeException or even an Error. Do it at places where you know, that
your application can continue execution without problems, even if that problem
occurs.

If your application needs a certain file, a FileNotFoundException is a much
bigger problem than a NullPointerException in your logo's animation routine :-)

HTH


Karl Schmidt

Kyungkoo Jun

unread,
Dec 7, 2000, 3:00:00 AM12/7/00
to
Karl Schmidt wrote:

Karl,,, Good point,, and I guess that's what I'd like to say too.
I mean it is not reasonable to catch error like VirtualMachineError or
OutOfMemoryError to contine pretending as nothing wrong,,,,
Unless you need to save something before stop.

KK.


Kyungkoo Jun

unread,
Dec 7, 2000, 11:28:24 AM12/7/00
to
What you caugtht in the example is "Exception", not an "Error".
But "Error" can be caught, but it should not be caught
by application,, because something really weird happened.

KK

Jon Skeet wrote:

> mark...@my-deja.com wrote:
> > > Same way you deal with any exception - use try and catch.
>
> > Are you kiding: I don't think that you can catch run-time error.
>
> No, I'm not kidding - you can catch them perfectly easily.
>
> Try this, for example:
>
> public class Test
> {
> public static void main (String [] args)
> {
> String s = null;
> try
> {
> s.length();
> System.out.println ("Strange...");
> }
> catch (NullPointerException e)
> {
> System.out.println ("Look, caught a runtime exception");
> }
> }
> }
>

Kyungkoo Jun

unread,
Dec 7, 2000, 11:32:20 AM12/7/00
to
I guess "run-time error" IN THE APPLICATION
can be caught,, if you read "catch" statement spec,,
it can catch any type of "Throwable" or subclasses,,
(Error is a subclass of Throwable)
but generally it should not be caught by application because
something really weird happened.

If I'm wrong, please correct me.

KK.

mark...@my-deja.com wrote:

> In article <MPG.149821f7...@10.1.1.51>,
> Jon Skeet <sk...@pobox.com> wrote:
> > mark...@my-deja.com wrote:
> > > I tried to load classes with forName but some classes I tried to
> > > loaded didn't have a default constructor. That caused a
> > > java.lang.NoClassDefFoundError which can't be cought.
> > >
> > > How can I get around this so that I don't load what I can not load?
> >

> > Same way you deal with any exception - use try and catch.
> >

> > --
> > Jon Skeet - sk...@pobox.com
> > http://www.pobox.com/~skeet
> >
>

> Are you kiding: I don't think that you can catch run-time error.
>

Andrew Owseiko

unread,
Dec 7, 2000, 7:18:05 PM12/7/00
to

> > mark...@my-deja.com wrote:
> > > I tried to load classes with forName but some classes I tried to
> > > loaded didn't have a default constructor. That caused a
> > > java.lang.NoClassDefFoundError which can't be cought.
Absence of default constructor is NOT the reason of
NoClassDefFoundError
try to run this code

public class Test
{
public static void main(String args[])throws Exception{
Class cl = Class.forName("clazz");
System.out.println("class "+cl.getName());
}
}
class clazz{
private clazz(int i){
}
}

> > >
> > > How can I get around this so that I don't load what I can not
load?
> >
> > Same way you deal with any exception - use try and catch.
> >
> > --
> > Jon Skeet - sk...@pobox.com
> > http://www.pobox.com/~skeet
> >
>
> Are you kiding: I don't think that you can catch run-time error.
>

Any subclass of java.lang.Throwable can be cought


java.lang.Object
|
+--java.lang.Throwable
|
+--java.lang.Error
|
+--java.lang.LinkageError
|
+--java.lang.NoClassDefFoundError

0 new messages