You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ben Nguyen <be...@gmail.com> on 2019/06/19 17:03:15 UTC

RE: [GSoC][STATISTICS][Regression][Exception] How should I handlehandling exceptions?

Hello,

Inside AbstractMultipleLinearRegression.java in commons.math3.stat.regression
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.InsufficientDataException;
import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.exception.NoDataException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.linear.NonSquareMatrixException;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  protected void validateSampleData(double[][] x, double[] y) throws MathIllegalArgumentException {
        if ((x == null) || (y == null)) {
            throw new NullArgumentException();
        }
        if (x.length != y.length) {
            throw new DimensionMismatchException(y.length, x.length);
        }
        if (x.length == 0) {  // Must be no y data either
            throw new NoDataException();
        }
        if (x[0].length + 1 > x.length) {
            throw new MathIllegalArgumentException(
                    LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
                    x.length, x[0].length);
        }
  }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I only have basic knowledge with exceptions, this is my first time hearing about checked vs unchecked exceptions, currently trying to learn more about it….
So with these exceptions, which I assume are mostly (not all) checked? How should I handle the porting of this specific method?

Thank you for your help,
-Ben


From: Alex Herbert
Sent: Wednesday, June 19, 2019 11:39 AM
To: Commons Developers List
Subject: Re: [GSoC][STATISTICS][Regression][Exception] How should I handlehandling exceptions?

On 19/06/2019 17:33, Ben Nguyen wrote:
> Hello,
> Regarding the use of exceptions for the regression module; should I simply try to port over all required exceptions straight from “apache.commons.math.exception” into it’s own exception package inside “regression.util.exception” perhaps customizing them to regression’s specific use cases as well….? Or will there be a full port of all exceptions for all modules to use…. though I do see one custom exception in “commons.distribution” so maybe not the latter?
>
> Thank you for your reply,
> Cheers,
> -Ben Nguyen
>
>

Math used checked exceptions.

I think the idea is to move to unchecked exceptions. In most cases this 
should be for illegal arguments and this is why the distribution module 
throws a special case of IllegalArgumentException. It is there to make 
formatting of messages easy.

What other types of exceptions are you expecting that cannot be 
satisfied by?

- IllegalStateException

- ArithmeticException

- Possibly others from the standard Java runtime exceptions

Alex



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



Re: [GSoC][STATISTICS][Regression][Exception] How should I handlehandling exceptions?

Posted by Alex Herbert <al...@gmail.com>.

> On 19 Jun 2019, at 18:03, Ben Nguyen <be...@gmail.com> wrote:
> 
> Hello,
> 
> Inside AbstractMultipleLinearRegression.java in commons.math3.stat.regression
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> 
> import org.apache.commons.math3.exception.DimensionMismatchException;
> import org.apache.commons.math3.exception.InsufficientDataException;
> import org.apache.commons.math3.exception.MathIllegalArgumentException;
> import org.apache.commons.math3.exception.NoDataException;
> import org.apache.commons.math3.exception.NullArgumentException;
> import org.apache.commons.math3.exception.util.LocalizedFormats;
> import org.apache.commons.math3.linear.NonSquareMatrixException;
> 
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> 
>  protected void validateSampleData(double[][] x, double[] y) throws MathIllegalArgumentException {
>        if ((x == null) || (y == null)) {
>            throw new NullArgumentException();
>        }
>        if (x.length != y.length) {
>            throw new DimensionMismatchException(y.length, x.length);
>        }
>        if (x.length == 0) {  // Must be no y data either
>            throw new NoDataException();
>        }
>        if (x[0].length + 1 > x.length) {
>            throw new MathIllegalArgumentException(
>                    LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
>                    x.length, x[0].length);
>        }
>  }
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> 
> I only have basic knowledge with exceptions, this is my first time hearing about checked vs unchecked exceptions, currently trying to learn more about it….
> So with these exceptions, which I assume are mostly (not all) checked? How should I handle the porting of this specific method?
> 
> Thank you for your help,
> -Ben
> 

These are all because the arguments are wrong. So IllegalArgumentException could be used for all of them. Then you document all the reasons why IllegalArgumentException is thrown in the Javadoc for the public method that could generate them (either the class constructor or the method that accepts the data).

You can substitute LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS for a string for now:

"not enough data ({0} rows) for this many predictors ({1} predictors)"

Becomes:

"not enough data (“ + x.length + " rows) for this many predictors (“ +x[0].length + " predictors)"

I do not know if a decision has been made on supporting localisation. I’ll wait for others to chime in.

> 
> From: Alex Herbert
> Sent: Wednesday, June 19, 2019 11:39 AM
> To: Commons Developers List
> Subject: Re: [GSoC][STATISTICS][Regression][Exception] How should I handlehandling exceptions?
> 
> On 19/06/2019 17:33, Ben Nguyen wrote:
>> Hello,
>> Regarding the use of exceptions for the regression module; should I simply try to port over all required exceptions straight from “apache.commons.math.exception” into it’s own exception package inside “regression.util.exception” perhaps customizing them to regression’s specific use cases as well….? Or will there be a full port of all exceptions for all modules to use…. though I do see one custom exception in “commons.distribution” so maybe not the latter?
>> 
>> Thank you for your reply,
>> Cheers,
>> -Ben Nguyen
>> 
>> 
> 
> Math used checked exceptions.
> 
> I think the idea is to move to unchecked exceptions. In most cases this 
> should be for illegal arguments and this is why the distribution module 
> throws a special case of IllegalArgumentException. It is there to make 
> formatting of messages easy.
> 
> What other types of exceptions are you expecting that cannot be 
> satisfied by?
> 
> - IllegalStateException
> 
> - ArithmeticException
> 
> - Possibly others from the standard Java runtime exceptions
> 
> Alex
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
> 
> 


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


Re: [GSoC][STATISTICS][Regression][Exception] How should I handlehandling exceptions?

Posted by Gilles Sadowski <gi...@gmail.com>.
Hi.

Le mer. 19 juin 2019 à 19:03, Ben Nguyen <be...@gmail.com> a écrit :
>
> Hello,
>
> Inside AbstractMultipleLinearRegression.java in commons.math3.stat.regression
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> import org.apache.commons.math3.exception.DimensionMismatchException;
> import org.apache.commons.math3.exception.InsufficientDataException;
> import org.apache.commons.math3.exception.MathIllegalArgumentException;
> import org.apache.commons.math3.exception.NoDataException;
> import org.apache.commons.math3.exception.NullArgumentException;
> import org.apache.commons.math3.exception.util.LocalizedFormats;
> import org.apache.commons.math3.linear.NonSquareMatrixException;
>
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
>   protected void validateSampleData(double[][] x, double[] y) throws MathIllegalArgumentException {
>         if ((x == null) || (y == null)) {
>             throw new NullArgumentException();
>         }
>         if (x.length != y.length) {
>             throw new DimensionMismatchException(y.length, x.length);
>         }
>         if (x.length == 0) {  // Must be no y data either
>             throw new NoDataException();
>         }
>         if (x[0].length + 1 > x.length) {
>             throw new MathIllegalArgumentException(
>                     LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
>                     x.length, x[0].length);
>         }
>   }
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> I only have basic knowledge with exceptions, this is my first time hearing about checked vs unchecked exceptions, currently trying to learn more about it….
> So with these exceptions, which I assume are mostly (not all) checked?

No; all are unchecked (aka subclasses of "RuntimeException").

> How should I handle the porting of this specific method?

Look at how it is done in e.g. "Commons Numbers".
The current consensus is to only advertise (public API) standard JDK exceptions.
Custom exception should be instantiated through a "factory".

Regards,
Gilles

>
> Thank you for your help,
> -Ben
>
>
> From: Alex Herbert
> Sent: Wednesday, June 19, 2019 11:39 AM
> To: Commons Developers List
> Subject: Re: [GSoC][STATISTICS][Regression][Exception] How should I handlehandling exceptions?
>
> On 19/06/2019 17:33, Ben Nguyen wrote:
> > Hello,
> > Regarding the use of exceptions for the regression module; should I simply try to port over all required exceptions straight from “apache.commons.math.exception” into it’s own exception package inside “regression.util.exception” perhaps customizing them to regression’s specific use cases as well….? Or will there be a full port of all exceptions for all modules to use…. though I do see one custom exception in “commons.distribution” so maybe not the latter?
> >
> > Thank you for your reply,
> > Cheers,
> > -Ben Nguyen
> >
> >
>
> Math used checked exceptions.
>
> I think the idea is to move to unchecked exceptions. In most cases this
> should be for illegal arguments and this is why the distribution module
> throws a special case of IllegalArgumentException. It is there to make
> formatting of messages easy.
>
> What other types of exceptions are you expecting that cannot be
> satisfied by?
>
> - IllegalStateException
>
> - ArithmeticException
>
> - Possibly others from the standard Java runtime exceptions
>
> Alex
>
>

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