Checked exceptions and lambdas
dev.to·1h·
Discuss: DEV
🛡️Error Handling
Preview
Report Post

Java’s checked exceptions were a massive improvement over C’s error-handling mechanism. As time passed and experience accumulated, we collectively concluded that we weren’t there yet. However, Java’s focus on stability has kept checked exceptions in its existing API.

Java 8 brought lambdas after the "checked exceptions are great" trend. None of the functional interface methods accepts a checked exception. In this post, I will demonstrate three different approaches to making your legacy exception-throwing code compatible with lambdas.

The problem, in code

Consider a simple exception-throwing method.

public class Foo {
public String throwing(String input) throws IOException {
return input;                          //1
}
}

  1. The body is there for compilation purpos…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help