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.
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.
Mark
> 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");
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.
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
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.
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");
> }
> }
> }
>
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.
>
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