Section 7 · 29 questions

Exceptions

29 interview questions and answers in the Exceptions section.

English Exceptions Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. What is the difference between checked and unchecked exceptions?
  2. What is a checked exception and when to use it?
  3. What is an unchecked exception (Runtime Exception)?
  4. Which exceptions must be handled?
  5. What is at the top of the exception hierarchy?
  6. What is Throwable?
  7. What is the difference between Error and Exception?
  8. Is the execution of a finally block guaranteed?
  9. What is try-with-resources?
  10. What are the requirements for resources in try-with-resources?
  11. What is the AutoCloseable interface?
  12. What is the difference between AutoCloseable and Closeable?
  13. Can you create custom exceptions?
  14. When should you create your own exceptions?
  15. What is a stack trace?
  16. What is better: extend Exception or RuntimeException?
  17. What does the printStackTrace() method do?
  18. How to properly log exceptions?
  19. What is exception wrapping (wrapping)?
  20. Why you should not swallow exceptions (catch empty)?
  21. What does the throws keyword do?
  22. Can you throw a checked exception from a method without throws?
  23. What happens if an exception also occurs in the finally block?
  24. What are suppressed exceptions?
  25. Can you have multiple catch blocks for one try?
  26. What is multi-catch (catching multiple exceptions)?
  27. In what order should catch blocks be arranged?
  28. Can you rethrow an exception?
  29. What is exception chaining?

Study navigator

29 questions for interview preparation for Middle Java Developer.


All Questions

# Question Difficulty
1 What is the difference between checked and unchecked exceptions Medium
2 What is a checked exception and when to use it Medium
3 What is an unchecked exception (Runtime Exception) Medium
4 Which exceptions must be handled Easy
5 What is at the top of the exception hierarchy Medium
6 What is Throwable Medium
7 What is the difference between Error and Exception Medium
8 Is the execution of a finally block guaranteed Medium
9 What is try-with-resources Medium
10 What are the requirements for resources in try-with-resources Medium
11 What is the AutoCloseable interface Medium
12 What is the difference between AutoCloseable and Closeable Medium
13 Can you create custom exceptions Easy
14 When should you create your own exceptions Medium
15 What is better: extend Exception or RuntimeException Medium
16 What is a stack trace Medium
17 What does the printStackTrace() method do Easy
18 How to properly log exceptions Medium
19 What is exception wrapping Medium
20 Why you should not swallow exceptions (empty catch) Medium
21 What does the throws keyword do Easy
22 Can you throw a checked exception from a method without throws Hard
23 What happens if an exception also occurs in the finally block Hard
24 What are suppressed exceptions Hard
25 Can you have multiple catch blocks for one try Easy
26 What is multi-catch Medium
27 In what order should catch blocks be arranged Medium
28 Can you rethrow an exception Medium
29 What is exception chaining Medium

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                 │
                    └──────────────────────────────────────────┘

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