Re: To check or not to check for NULL?
Mike wrote:
So don't catch it (the Error- OutOfMemoryError ) all? And why would I
The API defines "Error" to indicate "serious problems that a reasonable
application should not try to catch."
Other Throwables, Exceptions and RuntimeExceptions, are for problems, possibly
serious, that a reasonable application should try to catch.
need to catch the exception as the other person suggested? I thought
If an Exception is thrown, it should be caught, or at least handled.
new operator throws an Error, not an Exception.
The 'new' operator isn't the only thing happening - the object's constructor
can very well throw an Exception. Not only can it throw any checked exception
declared in the signature, it can throw an unchecked exception, intentionally
or unintentionally.
public class Foo
{
/** Held number. */
public final int len;
/** Constructor.
* @param len <code>String</code> source of held number.
*/
public Foo( String arg )
{
len = arg.length(); // risks NullPointerException
}
}
I'm not sure why Hakan suggested catching an Exception.
If an Exception is thrown, it should be caught, or at least handled.
Any client of the 'Foo' class above had best deal with NullPointerExceptions
or take pains that they don't happen.
--
Lew