Commit 092e9b7
committed
feat(exceptions): add notes and example on using generics with custom exceptions
WHAT:
- Explained why generic exception classes like `class MyGenericException<T> extends Exception` are not allowed in Java.
- Added example of `DetailedException<T>` that uses generics safely by keeping type information in fields rather than the class itself.
- Demonstrated throwing and catching a `DetailedException<Integer>` with additional error details.
WHY:
- In Java, all exceptions extend from `Throwable`. For runtime consistency, the JVM must know the concrete exception type.
- Due to **type erasure**, generic type parameters (like `<T>`) are erased at runtime, so the JVM cannot distinguish between
`MyGenericException<Integer>` and `MyGenericException<String>`.
- This makes generic exception instantiation impossible, but we can still use generics to enrich exception details.
HOW:
- Defined `class DetailedException<T> extends Exception` with a generic field `T details`.
- Constructor accepts both a message (`String`) and details (`T`).
- `getDetails()` returns the type-safe extra data.
- Example: throwing `new DetailedException<Integer>("An error occurred", 404)` and catching it to retrieve both the message and extra detail.
KEY TAKEAWAYS:
1. **Checked vs Unchecked Exceptions**
- `Exception` (checked): must be declared or handled.
- `RuntimeException` (unchecked): don’t need explicit declaration/handling.
2. **Restrictions with Generics**
- Cannot define `class MyGenericException<T> extends Exception` → compile-time error.
- Reason: **type erasure** removes `<T>` at runtime.
3. **Safe Pattern**
- Store generic data inside an exception (fields), not in its type.
- This allows passing extra type-safe diagnostic data while keeping exception hierarchy valid.
REAL-WORLD USE CASES:
- Wrapping domain-specific errors with structured details:
- Example: `DetailedException<OrderId>` for invalid order references.
- Example: `DetailedException<ErrorCode>` in APIs for passing numeric error codes alongside messages.
- Improves debugging and error-handling workflows by attaching context data directly to exceptions.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 216a685 commit 092e9b7
File tree
1 file changed
+78
-0
lines changed- Section24JavaGenerics/src/GenericExceptions
1 file changed
+78
-0
lines changedLines changed: 78 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
0 commit comments