I would write the idiomatic Java this way:

class XY implements FixedInterface {

    public int execute() {
        try {
            int x = getMeAnX();
            int y = getMeAY();

            return x + y;
        } catch (NoXException e) {
            throw new RuntimeException(e);
        } catch (NoYException e) {
            throw new RuntimeException(e);
        }
    }
}

so that the non-exceptional flow is not masked by the treatment of
exceptions.
Maybe is a matter of taste, but for me it seems more readable.
I would use the initial layout only if I had something special to do for
every exception something like:

class XY implements FixedInterface {
    public int execute() {
        int x;
        try {
            x = getMeAnX();
        } catch (NoXException e) {
            //do something special here
           //some special processing ?
            throw new RuntimeException(e);
        }
        int x1;
        try {
            x1 = getMeAnX();
        } catch (NoXException e) {
            //do something extra-special here
            //extra special processing
            throw new RuntimeException(e);
        }
        int y;
        try {
            y = getMeAY();
        } catch (NoYException e) {
            throw new RuntimeException(e);
        }
        return x + x1 + y;
    }
}

Regarding the issue of checked exceptions I'd say that all of bad things
about them stems from IOException and SQLException.
Those 2 have tormented a lot of programmers and are the poster childs of bad
checked exception usage.

My 2 cents,
Edward.

P.S. : This debate sounds a lot like the one about OO and Functional, btw.
P.P.S. : damn, it's so hard to write code in the GMail editor ;)


On Thu, Mar 24, 2011 at 5:54 PM, Ricky Clarkson <[email protected]>wrote:

> Obviously, suggesting that Maybe/Option is a better solution is equally
>> silly.
>>
>
> It is a better solution, because it doesn't have to affect readability the
> same way.  Consider the difference between:
>
> Go (probably incorrect, I don't speak that language):
>
> x, err = getMeAnX()
> if (isError(err))
>     return -1, err
> y, err = getMeAY()
> if (isError(err))
>     return -2, err
> return x + y
>
>  Idiomatic Java:
>
> class XY implements FixedInterface {
>     public int execute() {
>         int x;
>         try {
>             x = getMeAnX();
>         } catch (NoXException e) {
>             throw new RuntimeException(e);
>         }
>         int y;
>         try {
>             y = getMeAY();
>         } catch (NoYException e) {
>             throw new RuntimeException(e);
>         }
>         return x + y;
>     }
> }
>
> Java with Option:
>
> class XPlusY implements FixedInterface {
>     public Option<Integer> execute() {
>         for (int x: getMeAnX())
>             for (int y: getMeAY())
>                 return Option.some(x + y);
>         return Option.none();
>     }
> }
>
> Scala, just for fun:
>
> def execute = for (x <- getMeAnX; y <- getMeAY) yield x + y
>
> or
>
> def execute = getMeAnX.bind(x => getMeAY.map(y => x + y))
>
> The line above can be translated to Java fairly mechanically but I'm not
> sure that would add anything to the debate apart from lots of lines.
>
>
>> --
>> Cédric
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "The Java Posse" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/javaposse?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to