Quick revision
The short answer
I would define a small set of named functional interfaces that declare a generic exception type, such as interface IoFunction<T, R, E extends Exception> { R apply(T t) throws E; }, and make the library methods declare throws E, so the caller's own IOException or SQLException comes out with its real type and the compiler checks it. The alternative is to accept standard interfaces and document that callers wrap their exceptions in unchecked ones. Either way, the number of custom interfaces stays small, and we test that callers can still pass method references.
What to remember
In this answer, we designed functional parameters for a shared library whose callers throw checked exceptions. Named interfaces with a generic exception parameter, such as CheckedFunction<T, R, E extends Exception>, let library methods declare throws E so the caller's IOException or SQLException comes out with its real type, and the compiler infers RuntimeException when nothing is thrown. The alternative is standard interfaces with documented unchecked wrappers. Either way we keep the number of custom interfaces small, keep checked callbacks synchronous, and test that method references still work.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would define a small set of named functional interfaces that declare a generic exception type, such as interface IoFunction<T, R, E extends Exception> { R apply(T t) throws E; }, and make the library methods declare throws E, so the caller's own IOException or SQLException comes out with its real type and the compiler checks it. The alternative is to accept standard interfaces and document that callers wrap their exceptions in unchecked ones. Either way, the number of custom interfaces stays small, and we test that callers can still pass method references.
Why this question is asked
A shared data-processing library sits between many teams and their data sources, and those data sources throw checked exceptions all the time. If the library's functional parameters are plain Function and Consumer, every caller has to wrap and unwrap exceptions, and many will do it badly. If the library invents its own interfaces carelessly, callers face a zoo of types. Interviewers ask this to see whether we know the generic exception technique and its limits. In this answer, we will recap the problem, design interfaces with a generic exception parameter, see how the compiler infers the exception type, look at the main alternative, and finish with the rules that keep the API small and usable.
1A Quick Recap of the Problem
Our library offers operations such as processAll(source, transform, sink), where the caller supplies the transform and sink as lambdas. Callers' lambdas read files, query databases and call services, so they throw IOException or SQLException, which are checked exceptions that the compiler forces us to catch or declare. The standard java.util.function interfaces cannot throw checked exceptions, so with those types every caller must wrap inside the lambda and unwrap afterwards. The goal is an API where the caller's exception flows out naturally, keeps its type, and is still checked by the compiler.
2Interfaces With a Generic Exception Parameter
Java lets a type parameter appear in a throws clause. So we can declare a functional interface whose method throws whatever exception type the caller's lambda throws, and make the library method declare the same type.
Notice that inside the library we use a plain loop. The library code itself does not need streams, and a loop lets the exception propagate without any wrapping. The wildcards ? super T and ? extends R let callers pass functions that accept a wider input type or return a narrower one, which is what they expect from any well-written Java API.
3How the Compiler Infers the Exception Type
The interesting part is what E becomes at each call site. The compiler infers it from the exceptions the lambda body or the referenced method can throw.
| Lambda or method reference | Inferred E | Caller must handle |
|---|---|---|
Files::readString | IOException | IOException |
id -> dao.load(id) | SQLException | SQLException |
| String::length | RuntimeException | nothing |
| throws IOException and SQLException | Exception | Exception, less precise |
The third line is what makes this design pleasant: when the lambda throws no checked exception, Java infers E as RuntimeException, so callers who do not throw anything are not forced to write a try block. The fourth line shows the main limit. One type parameter can hold only one exception type, so a lambda that throws two unrelated checked exceptions makes E their common supertype, Exception, and the caller loses precision. That case is rare in practice and can be handled by wrapping one of them.
4Propagating the Exception Through the Library
Every public method that calls a caller-supplied function must declare throws E and must not catch it on the way. That is easy in synchronous code like mapAll. It becomes harder when the library runs callbacks lazily, in a stream, or on another thread, because the exception is thrown somewhere the method's throws clause cannot reach. In those places the library must catch the exception internally, carry it across, and rethrow it as E at the public method, which needs an unchecked cast and careful testing.
So a practical rule is that methods taking checked functional parameters do their work synchronously, on the caller's thread, and asynchronous methods use a different design, such as returning a CompletableFuture whose failure holds the original exception.
5The Alternative: Standard Interfaces Plus Documented Wrappers
The other reasonable design is to accept the standard Function and Consumer and say in the documentation that callers must wrap checked exceptions in unchecked ones, preferably UncheckedIOException or one shared wrapper type, and that the library will let those wrappers pass through untouched. This keeps the API surface tiny and works directly with every existing lambda and library. The cost is that the compiler no longer reminds callers about their exceptions, and each team needs to unwrap at its own boundary. For a library whose callers mostly do I/O, I prefer the generic exception design; for a general utility library, standard interfaces are fine.
6Keeping the API Small and Usable
Custom interfaces are only worth it if there are few of them. We would ship exactly the shapes the library needs, typically CheckedFunction, CheckedConsumer, CheckedSupplier and perhaps CheckedRunnable, all in one package, with no primitive variants unless profiling demands them. We would not create a second family for two exception parameters.
Finally, we test the API from the caller's side. A set of compile tests in the library's own test suite calls every public method with method references such as Files::readString, constructor references, overloaded methods and lambdas that throw nothing, and checks that each compiles and that the inferred exception is the one we expect. The reason is that overloaded methods and wildcards can make method references ambiguous in surprising ways, and we want to find that before the teams that use the library do. We also provide a one-line adapter, such as CheckedFunction.of(Function<T, R> f), so callers who already hold a standard Function can pass it in.
7Follow-up questions
Why does the compiler infer RuntimeException when nothing is thrown?
The Java Language Specification has a special rule for inference variables that appear only in throws clauses: if nothing forces them to be a checked type, they resolve to RuntimeException. That rule exists precisely to support this generic exception pattern, so callers without checked exceptions are not forced to catch anything.
Can these interfaces be used with Stream.map?
Not directly, because Stream.map expects a java.util.function.Function. That is fine for our library, which processes data with its own methods. If callers want to mix in streams, we provide a helper that converts a CheckedFunction into a Function by wrapping in an unchecked exception.
Should the exception bound be Exception or Throwable?
Exception is the better bound. Allowing Throwable would let callers declare Error types, which are not meant to be caught or declared, and it complicates the library's internal handling for no real benefit.
8Conclusion
In this answer, we designed functional parameters for a shared library whose callers throw checked exceptions. Named interfaces with a generic exception parameter, such as CheckedFunction<T, R, E extends Exception>, let library methods declare throws E so the caller's IOException or SQLException comes out with its real type, and the compiler infers RuntimeException when nothing is thrown. The alternative is standard interfaces with documented unchecked wrappers. Either way we keep the number of custom interfaces small, keep checked callbacks synchronous, and test that method references still work.
Open with
I'd give the library a small set of named functional interfaces with a generic exception parameter, for example CheckedFunction of T, R and E extends Exception, whose apply method throws E.
Then, if they want more
Public methods like mapAll take a CheckedFunction and declare throws E, and internally they use plain loops, so the caller's own exception comes out with its real type.
The nice part is inference: if the caller passes Files::readString, E becomes IOException and the compiler makes them handle it; if they pass a DAO call, it's SQLException; and if the lambda throws nothing, Java infers RuntimeException, so they don't need a try block at all.
The limit is that one type parameter holds one type, so a lambda throwing two unrelated checked exceptions infers Exception. I'd keep the callbacks synchronous on the caller's thread, because once a callback runs lazily or on another thread, throws E can't reach the caller and we'd need to carry the exception across.
The alternative design is to accept the standard Function and Consumer and document that callers wrap checked exceptions in UncheckedIOException or a shared wrapper, which the library passes through; that's simpler but loses compiler checking.
Whichever I pick, I'd keep the custom interfaces to three or four in one package, provide an adapter from standard Function, and have compile tests that call every method with method references, constructor references and overloaded methods to make sure callers can still use them.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are designing the public API of a shared data-processing library used by many teams....": https://scrivio-interview-library.netlify.app/java/lambdas/5e560a2670/