Re: Catching NPEs
Frank wrote:
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.
You can catch them, but it's not clear how you should
"respond" to them. Remember, the context where the exception
was thrown is long gone: the call stack has been unwound all
the way up to the level of your try/catch, any finally blocks
along the way have been executed, and so on. You can "respond"
by printing a message or something, but you do not have the
option to "repair and resume."
Also, catching Exception is an awfully dull tool: it catches
everything you want along with everything you don't want. If
you write catch(Exception e) you will catch NullPointerException,
but you will catch IllegalArgumentException and ArithmeticException
and a host of others along with it. Unless you are prepared to do
something intelligent about all these different Exceptions, it is
unlikely to be a good idea to spread your net quite so widely.
--
Eric Sosman
esosman@acm-dot-org.invalid