Except that's not how it would be done in Java.

try {
 return getMeAnX() + getMeAY();
}
catch(NoXException e) {
  throw new RuntimeException(e);
}
catch(NoYException e) {
  throw new RuntimeException(e);
}

 Alexey





________________________________
From: Ricky Clarkson <[email protected]>
To: [email protected]
Cc: Cédric Beust ♔ <[email protected]>; Russel Winder <[email protected]>
Sent: Thu, March 24, 2011 11:54:55 AM
Subject: Re: [The Java Posse] How to deal with CheckedExceptions


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