commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <gil...@harfang.homelinux.org>
Subject Re: [math] right way to throw IndexOutOfBoundsException?
Date Mon, 08 Aug 2011 17:01:01 GMT
On Mon, Aug 08, 2011 at 08:34:13AM -0700, Phil Steitz wrote:
> On 8/7/11 11:38 PM, Gilles Sadowski wrote:
> > On Sun, Aug 07, 2011 at 10:47:41PM -0700, Phil Steitz wrote:
> >> I was about to do this:
> >> throw MathRuntimeException.createArrayIndexOutOfBoundsException(
> >>                     LocalizedFormats.OUT_OF_RANGE_SIMPLE, index, 0,
> >> parameters.length);
> >>
> >> but remembered that this is now deprecated.  Do I need to add a
> >> MathArrayIndexOutOfBoundsException extending the JDK exception and
> >> then throw that directly?  
> > Do we really want to subclass all Java exceptions?
> > There was a discussion where I thought that we circumscribed all the types
> > of problems that would need CM exceptions to sublcass the standard ones.
> >
> > Cases such as this would fall in the "illegal argument" category.
> > Thus:
> >
> >   throw new OutOfRangeException(index, 0, parameters.length);
> >
> > or, to get a more detailed message,
> >
> >   OutOfRangeException e = new OutOfRangeException(index, 0, parameters.length);
> >   e.addMessage(INDEX, index);
> >   throw e;
> >
> >
> > Of course, "OutOfRangeException" cannot inherit from both
> > "IllegalArgumentException" and "IndexOutOfBoundsException"...
> 
> I thought about that, but would prefer to throw
> ArrayIndexOutOfBoundsException because that is really what is going
> on and I would prefer to throw the standard exception.  Ideally, I
> would like to throw that with a message reporting the value and the
> length of the array.  So, there are three choices:
> 
> 0) throw AIOB with no message
> 1) subclass and throw with localized message - my suggestion above
> 2) OutOfRangeException
> 
> I like 1) the best and since we have decided to deprecate the
> MathRuntimeException, note that it applies to all of the other
> standard exceptions generated by MathRuntimeException's createXxx
> methods that have not yet been subclassed.  I think we should follow
> the generally accepted practice to favor standard exceptions, so
> that means we are going to have to create wrappers for all that we
> use.  I am willing to help with this.  In this case, I will go ahead
> and add the MathArrayIndexOutOfBoundsException that will be an
> ArrayIndexOutOfBoundsException if others are OK with this.   Note
> that doing this will allow us to handle situations where IAE is not
> appropriate (essentially why AIOB does not itself extend IAE).

As I understand it, AIOB is a low-level exception that is thrown by the JVM
checking an array access:
---CUT---
  i = 3;
  double a = arr[i]; // <--- can throw AIOB
---CUT---

However, I don't see how a user code can be similar to this: When you check
the index in your above code, you didn't try to access the array yet. You've
detected that it won't work because the index value is "out of range", thus,
"illegal".


Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message