EasyExperience level: 0-2 years · 3-5 years1 min read

Checked vs unchecked exceptions — which do you use in a service layer, and why?

Reviewed by Gurusankar M.

Asked inCognizantAccentureInfosysDeloitte
#exceptions#checked#unchecked#runtimeexception#api design
Report issue

Short Answer

Checked = recoverable, caller must handle (compile-time). Unchecked (RuntimeException) = programming/unrecoverable errors. Modern service layers favour unchecked custom exceptions to avoid throws-clause pollution and leaky abstractions.

On this page

Concept Made Simple

Coffee Chat Question

“Checked vs unchecked exceptions — which do you use in a service layer, and why?”

Remember It Faster

Mind Map Answer

Checked
Exception subclasses — must catch or declare
Unchecked
RuntimeException — optional handling
Error
JVM-level (OOM) — don't catch

Teams wrap low-level checked exceptions (SQLException, IOException) in a domain RuntimeException so business code isn't littered with throws and stays decoupled from the data layer.

Think Beyond the Expected

What If?

Why do Spring and JPA use unchecked exceptions?

Spring translates SQLException into an unchecked DataAccessException hierarchy so your service/controller code isn't forced to catch or declare persistence details — keeping layers decoupled and code clean.

Real World

A payments service defines `PaymentDeclinedException extends RuntimeException`; a @ControllerAdvice handler maps it to HTTP 402 once, instead of every method declaring `throws`.

Interviewer's Expectation

Keywords they're listening for:

  • compile-time vs runtime
  • recoverable vs programming error
  • wrap & rethrow
  • Spring DataAccessException
  • don't catch Error

Common Mistakes

  • Swallowing exceptions with an empty catch block
  • Wrapping without passing the cause (losing the stack trace)
  • Using exceptions for normal control flow

Best Practices

  • Wrap-and-rethrow with the cause: throw new DomainException(msg, e)
  • Catch narrowly; never swallow
  • Centralize mapping via @ControllerAdvice / global handler

Follow-up Questions

  1. Why is catching Throwable or Exception dangerous?
  2. How do you preserve the original cause when re-throwing?
  3. Where do you centralize exception-to-HTTP mapping?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Exceptions (Core Java)
Interview question: "Checked vs unchecked exceptions — which do you use in a service layer, and why?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.