Exceptions
29 interview questions and answers in the Exceptions section.
Questions in this section
- What is the difference between checked and unchecked exceptions?
- What is a checked exception and when to use it?
- What is an unchecked exception (Runtime Exception)?
- Which exceptions must be handled?
- What is at the top of the exception hierarchy?
- What is Throwable?
- What is the difference between Error and Exception?
- Is the execution of a finally block guaranteed?
- What is try-with-resources?
- What are the requirements for resources in try-with-resources?
- What is the AutoCloseable interface?
- What is the difference between AutoCloseable and Closeable?
- Can you create custom exceptions?
- When should you create your own exceptions?
- What is a stack trace?
- What is better: extend Exception or RuntimeException?
- What does the printStackTrace() method do?
- How to properly log exceptions?
- What is exception wrapping (wrapping)?
- Why you should not swallow exceptions (catch empty)?
- What does the throws keyword do?
- Can you throw a checked exception from a method without throws?
- What happens if an exception also occurs in the finally block?
- What are suppressed exceptions?
- Can you have multiple catch blocks for one try?
- What is multi-catch (catching multiple exceptions)?
- In what order should catch blocks be arranged?
- Can you rethrow an exception?
- What is exception chaining?
Study navigator
29 questions for interview preparation for Middle Java Developer.
All Questions
Topic Dependency Map
┌──────────────────────────────────────────┐
│ HIERARCHY (5-7) │
│ 5. Top of hierarchy (Throwable) │
│ 6. Throwable (state, methods) │
│ 7. Error vs Exception │
└──────────────────┬───────────────────────┘
│
┌──────────────────────────┼──────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ CHECKED vs │ │ TRY-CATCH- │ │ CUSTOM │
│ UNCHECKED │ │ FINALLY │ │ EXCEPTIONS │
│ (1-4) │ │ (8-10) │ │ (13-15) │
│ 1. checked vs │ │ 8. finally │ │ 13. Can you │
│ unchecked │ │ 9. try-with │ │ create │
│ 2. checked │ │ resources │ │ 14. When to │
│ 3. unchecked │ │ 10. Resources │ │ create │
│ 4. Required │ │ reqs │ │ 15. Exception vs │
│ handling │ │ │ │ RuntimeException
└───────┬───────┘ └───────┬───────┘ └────────┬───────────┘
│ │ │
└────────────────────────┼────────────────────────┘
▼
┌──────────────────────────────────────────┐
│ RESOURCES & AUTO-CLOSE (11-12) │
│ 11. AutoCloseable │
│ 12. AutoCloseable vs Closeable │
└──────────────────────────────────────────┘
┌──────────────────────────────────────────┐
│ HANDLING & LOGGING (16-21) │
│ 16. Stack trace │
│ 17. printStackTrace() │
│ 18. Logging │
│ 19. Wrapping │
│ 20. Empty catch (swallowing) │
│ 21. throws │
└──────────────────────────────────────────┘
┌──────────────────────────────────────────┐
│ ADVANCED TOPICS (22-29) │
│ 22. Sneaky throws (type erasure) │
│ 23. finally throws │
│ 24. Suppressed exceptions │
│ 25. Multiple catch blocks │
│ 26. Multi-catch │
│ 27. Catch block order │
│ 28. Rethrow │
│ 29. Exception chaining │
└──────────────────────────────────────────┘
Recommended Study Order
Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Hierarchy | Q5, Q6, Q7 | Throwable, Error/Exception, differences |
| 2 | Checked vs Unchecked | Q1, Q2, Q3 | Differences, when to use what |
| 3 | Required handling | Q4 | Which exceptions the compiler enforces |
| 4 | try-catch-finally | Q8 | finally executes (almost) always |
| 5 | try-with-resources | Q9, Q10 | Auto-closing resources, LIFO |
| 6 | Custom exceptions | Q13, Q14 | When to create custom ones |
Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | AutoCloseable vs Closeable | Q11, Q12 | Idempotency, flush, when to use which |
| 2 | Exception vs RuntimeException | Q15 | Decision tree for choosing |
| 3 | Stack trace | Q16, Q17 | How to read, why printStackTrace is bad |
| 4 | Logging | Q18 | SLF4J {}, MDC, PII caveats |
| 5 | Wrapping and rethrow | Q19, Q20, Q28 | Chain of causality, when to wrap |
| 6 | throws | Q21 | Method contract, checked vs unchecked |
| 7 | Multi-catch and order | Q25, Q26, Q27 | First match wins, when multi-catch |
Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Sneaky throws | Q22 | Type erasure, when it is dangerous |
| 2 | finally + exception | Q23 | Shadowing mechanism, how to preserve |
| 3 | Suppressed exceptions | Q24 | Java 7+, TWR mechanism, addSuppressed |
| 4 | Exception chaining deep dive | Q29 | Cause chain, logging impact |
| 5 | Performance | Q16 (Senior) | fillInStackTrace cost, StackWalker, OmitStackTraceInFastThrow |
| 6 | Production patterns | Q18 (Senior) | Log levels, alert fatigue, duplicate logging |
Key Connections Between Topics
Topic: Hierarchy
Q5 (Top), Q6 (Throwable) , Q7 (Error vs Exception)
|
Q1 (checked vs unchecked) , Q2 (checked) , Q3 (unchecked)
Key connections:
- Q6 <-> Q8: Throwable stores stack trace, finally can “shadow” it
- Q7 <-> Q4: Error should not be caught, Exception (checked) - mandatory
Topic: try-catch-finally
Q8 (finally) , Q9 (try-with-resources) , Q10 (Resource requirements)
| |
Q23 (finally throws) , Q24 (Suppressed)
Key connections:
- Q8 <-> Q23: finally can “shadow” exception from try
- Q9 <-> Q24: suppressed exceptions appeared with try-with-resources (Java 7)
- Q10 <-> Q11: Resource requirements = AutoCloseable + idempotency
Topic: Custom Exceptions
Q13 (Can you) , Q14 (When to create) , Q15 (Exception vs RuntimeException)
Key connections:
- Q14 <-> Q3: Domain unchecked exceptions - expected business situations
- Q15 <-> Q2: Decision tree for checked vs unchecked
Topic: Handling and Logging
Q16 (Stack trace) , Q17 (printStackTrace) , Q18 (Logging)
| |
Q19 (Wrapping) , Q20 (Empty catch) , Q29 (Exception chaining)
Key connections:
- Q17 <-> Q18: printStackTrace to System.err, logger to file with context
- Q19 <-> Q29: Wrapping = creating a chain of causes
- Q20 <-> Q18: Swallowing exceptions = not logging = losing information
Topic: Advanced Techniques
Q22 (Sneaky throws) , Q21 (throws)
Q23 (finally throws) , Q24 (Suppressed)
Q25 (Multiple catch) , Q26 (Multi-catch) , Q27 (Catch order)
Key connections:
- Q22 <-> Q1: Sneaky throws bypasses checked/unchecked separation
- Q23 <-> Q8: finally can “shadow” the original exception
- Q24 <-> Q9: Suppressed appeared with try-with-resources
- Q25-Q27: first match wins, multi-catch = Java 7+ alternative
Cheat Sheet: What to Know for Each Level
Junior
- Throwable - root of hierarchy, Error vs Exception
- Checked = compiler forces handling, unchecked = no
- finally executes (almost) always
- try-with-resources = auto-closing, resource must be AutoCloseable
- Don’t create custom exceptions without necessity
- printStackTrace - only for quick debugging, not for production
Middle
- Fail-Fast: unchecked exceptions manifest early, don’t “leak”
- @ControllerAdvice - centralized handling in Spring
- LIFO close order in TWR
- SLF4J:
logger.error("msg", e)- last argument is Throwable - Wrapping: wrap preserving cause (
new Exception("msg", cause)) - Multi-catch (Java 7+):
catch (A | B e)- types must not be in inheritance relation - first match wins: catch from general to specific
Senior
- Sneaky throws: type erasure deceives compiler, dangerous for API
- Shadowing: exception from finally “shadows” the one from try - use addSuppressed
- Suppressed exceptions: TWR mechanism, Throwable[] (not List!)
- fillInStackTrace: native method, OS stack traversal, ~1-5 microseconds
- StackWalker (Java 9+): lazy access to stack frames, filtering
- OmitStackTraceInFastThrow: JVM optimizes “hot” exceptions without stack trace
- PII in logs: GDPR/PCI DSS violations, mask data
Format of Each File
Each file contains:
- Junior Level - basic understanding, simple analogies, examples
- Middle Level - internals, typical mistakes, practical examples
- Senior Level - deep dive, edge cases, production experience, monitoring
- Interview Cheat Sheet - key theses, frequent questions, red flags, related topics