Easy👤 0-2 years👤 3-5 years 1 min read

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

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.

Coffee Chat Question

Concept Made Simple

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

🧠Mind Map Answer

Remember It Faster

CheckedException subclasses — must catch or declare
UncheckedRuntimeException — optional handling
ErrorJVM-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.

🔥What If?

Think Beyond the Expected

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 runtimerecoverable vs programming errorwrap & rethrowSpring DataAccessExceptiondon'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

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

🧩Related Technologies

Spring DataAccessException@ControllerAdviceSLF4Jtry-with-resources

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions