#37 IllegalStateException should propagate original error

JGAP 3.2
closed-fixed
Klaus
3
2007-02-27
2007-02-16
slatemine
No

Coding error is a little harsh, but in Genotype, line 420

try
{
for (int i = 0; i < populationSize; i++)
{
pop.addChromosome( .....
}

}
catch (Exception ex)
{
throw new IllegalStateException(ex.getMessage());
}

is more helpful as:

try
{
for (int i = 0; i < populationSize; i++)
{
pop.addChromosome( .....
}

}
catch (Exception ex)
{
throw new IllegalStateException(ex.getMessage(), ex);
}

The getMessage() is non uniformly used whereas from 1.4 get cause i believe is. Including the original exception allowed me to find where my error code was, whereas without it my errors are hidden from me.

Probably happens elsewhere aswell.

cheers

p

Discussion

  • slatemine
    slatemine
    2007-02-16

    Changed genotype.java at line 420

     
    Attachments
  • Klaus
    Klaus
    2007-02-16

    Logged In: YES
    user_id=722855
    Originator: NO

    You may be right with your suggestion. But: Your proposal does not work with Java 1.4. JGAP is fully compatible with Java 1.4 (except the Robocode example). It would not be optimal wiping away this compatibility by just making a small change.

    When JGAP is "ported" to 1.5, I will happily consider your suggestion.

    Are there any comments or other opinions to this?

    Best

    Klaus
    PS: Great that you posted your suggestion! Reporting errors is one of the most important aspects in JGAP!

     
  • Klaus
    Klaus
    2007-02-16

    • milestone: 659916 --> JGAP 3.2
    • priority: 5 --> 3
    • assigned_to: nobody --> klausikm
     
  • slatemine
    slatemine
    2007-02-19

    Logged In: YES
    user_id=1644996
    Originator: YES

    Fair enough point re 1.4/1.5 compatibility, so i guess the only solution is to fully unpack the the original exception to ensure the cause is propagated. There probably needs to be a bit of code that checks if the getMessage() is populated and if not, starts to unpack the getCause() branch. Include all this in the in the string constructor for IllegalStateException( fullMessage ). I believe the standard in 1.4 is the getClause is always populated.

     
  • Klaus
    Klaus
    2007-02-19

    Logged In: YES
    user_id=722855
    Originator: NO

    OK, only problem in Java 1.4: If a throwable has cause == this, then null is returned for getCause(). SO I would suggest the quite lengthy code:
    ---
    StackTraceElement[] ste = ex.getStackTrace();
    String s = "";
    for (int i = 0; i < ste.length; i++) {
    if (i > 0) {
    s += "\n";
    }
    s += ste[i].toString();
    }
    throw new IllegalStateException(s);
    }
    else {
    throw new IllegalStateException(ex.getCause().toString());
    }
    ---

    Maybe using StringBuffer instead of String.

    Klaus

     
  • Klaus
    Klaus
    2007-02-27

    • status: open --> closed-fixed