Re: Why no "cause" constructors for NumberFormatException
Lew wrote:
dvdavins@pobox.com wrote:
On Jul 10, 8:24 am, Patricia Shanahan <p...@acm.org> wrote:
I'm not so sure of that. Does a Throwable cause really make sense, given
the definition of NumberFormatException?
Yes. For instance, since you can't set the message, the only way to
effectively do so is to catch the oroginal NumberFormatException and
to throw a new one with the message that's needed. It would be good to
then include the original as the cause. (Better, IMO, would be to be
able to set the message directly, but that's a broader issue with
Throwable.)
This all begs the questions of why you're rethrowing the exception (or one
based on it) instead of logging and handling it, and why you need a different
message if you haven't handled the Exception, and why you have to throw
NumberFormatException instead of a custom application-specific (possibly
checked) Exception since you aren't handling it at first catch, and why you
are accepting possibly invalid number formats for conversion instead of
prevalidating them in the first place, for all of which we will stipulate that
you have good reasons for bucking the best-practices trend. For others
reading this thread, these questions might be relevant.
--
Lew