Skip to content

Instantly share code, notes, and snippets.

@szarnekow
Created December 13, 2012 20:35
Show Gist options
  • Save szarnekow/4279571 to your computer and use it in GitHub Desktop.
Save szarnekow/4279571 to your computer and use it in GitHub Desktop.
Musings about checked exceptions in Xtend
import static extension Throwables.*
val uri = [| new URI(requestURI) ].onException [
new IllegalArgumentException(it)
]
try {
URI uri = new URI(requestUri);
..
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
URI uri = Throwables.propagate(
() -> new URI(requestUri));
// or even
URI uri = Throwables.propagate(
() -> new URI(requestUri),
IllegalArgumentException::new);
URI uri = Throwables.propagate(
() -> new URI(requestUri),
(e) -> new IllegalArgumentException(e));
class Throwables {
def static <T> T onException(
()=>T proc,
(Exception)=>Exception handler) {
try {
proc.apply
} catch(Exception e) {
throw handler.apply(e)
}
}
}
val uri = new URI(requestURI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment