exceptions: Java exception handling companion code (suppression, chaining, cost)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KqJyCidz3ZgRyHABv2GVJh
This commit is contained in:
2026-09-30 18:45:53 +00:00
co-authored by Claude Sonnet 5.5
parent 4a74153d87
commit bfa7f97432
32 changed files with 688 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
=== try/finally: the close() exception replaces the query() exception ===
caught: java.lang.IllegalArgumentException: close failed on db-1
suppressed count: 0
=== try-with-resources: query() exception wins, close() exception is attached ===
caught: java.lang.IllegalStateException: query failed on db-1
suppressed count: 1
suppressed: java.lang.IllegalArgumentException: close failed on db-1
=== two resources: closed in reverse order, each failure is suppressed ===
caught: java.lang.IllegalStateException: query failed on B
suppressed: java.lang.IllegalArgumentException: close failed on B
suppressed: java.lang.IllegalArgumentException: close failed on A
=== what printStackTrace() shows for suppressed exceptions ===
java.lang.IllegalStateException: query failed on db-1
at com.ankurm.exceptions.SuppressedDemo$Conn.query(SuppressedDemo.java:9)
Suppressed: java.lang.IllegalArgumentException: close failed on db-1
at com.ankurm.exceptions.SuppressedDemo$Conn.close(SuppressedDemo.java:10)
+20
View File
@@ -0,0 +1,20 @@
=== wrapped WITH the cause ===
getMessage(): could not load order 42
getCause(): java.lang.IllegalStateException: connection reset by peer
java.lang.RuntimeException: could not load order 42
at com.ankurm.exceptions.ChainingDemo.serviceGood(ChainingDemo.java:10)
at com.ankurm.exceptions.ChainingDemo.print(ChainingDemo.java:20)
at com.ankurm.exceptions.ChainingDemo.main(ChainingDemo.java:30)
Caused by: java.lang.IllegalStateException: connection reset by peer
at com.ankurm.exceptions.ChainingDemo.dao(ChainingDemo.java:6)
at com.ankurm.exceptions.ChainingDemo.serviceGood(ChainingDemo.java:9)
... 2 more
=== wrapped WITHOUT the cause (message copied as a string) ===
getMessage(): could not load order 42: connection reset by peer
getCause(): null
java.lang.RuntimeException: could not load order 42: connection reset by peer
at com.ankurm.exceptions.ChainingDemo.serviceBad(ChainingDemo.java:15)
at com.ankurm.exceptions.ChainingDemo.print(ChainingDemo.java:20)
at com.ankurm.exceptions.ChainingDemo.main(ChainingDemo.java:31)
@@ -0,0 +1,15 @@
=== which are checked? ===
Exception CHECKED Exception -> Throwable -> Object
IOException CHECKED IOException -> Exception -> Throwable -> Object
InterruptedException CHECKED InterruptedException -> Exception -> Throwable -> Object
RuntimeException unchecked RuntimeException -> Exception -> Throwable -> Object
IllegalArgumentException unchecked IllegalArgumentException -> RuntimeException -> Exception -> Throwable -> Object
NullPointerException unchecked NullPointerException -> RuntimeException -> Exception -> Throwable -> Object
Error unchecked Error -> Throwable -> Object
OutOfMemoryError unchecked OutOfMemoryError -> VirtualMachineError -> Error -> Throwable -> Object
StackOverflowError unchecked StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object
=== checked exception inside a stream lambda: wrap in UncheckedIOException ===
contents of a.txt
caught: java.io.UncheckedIOException: java.io.IOException: cannot read bad.txt
original cause: java.io.IOException: cannot read bad.txt
@@ -0,0 +1,5 @@
404-ish -> NOT_FOUND: order 42 does not exist
generic -> CONFLICT: version mismatch
ControlFlowException stack trace length: 0
RuntimeException stack trace length: 1
ControlFlowException suppressed count after addSuppressed(): 0
+1
View File
@@ -0,0 +1 @@
swallowed() returned -1 - the IllegalStateException never reached the caller
@@ -0,0 +1,4 @@
src/broken/LambdaChecked.java:8: error: unreported exception IOException; must be caught or declared to be thrown
Stream.of("a", "b").map(n -> read(n)).forEach(System.out::println);
^
1 error
@@ -0,0 +1,4 @@
src/broken/UnreachableCatch.java:8: error: exception IOException is never thrown in body of corresponding try statement
} catch (IOException e) {
^
1 error
@@ -0,0 +1,5 @@
src/broken/MultiCatchSubclass.java:8: error: Alternatives in a multi-catch statement cannot be related by subclassing
try { f(); } catch (FileNotFoundException | IOException e) { }
^
Alternative FileNotFoundException is a subclass of alternative IOException
1 error
@@ -0,0 +1,5 @@
src/broken/WiderOverride.java:7: error: run() in Impl cannot implement run() in Task
public void run() throws IOException { throw new IOException(); }
^
overridden method does not throw IOException
1 error
@@ -0,0 +1,5 @@
$ java OmitStackTraceDemo
the ArithmeticException at iteration 1127 had an EMPTY stack trace
$ java -XX:-OmitStackTraceInFastThrow OmitStackTraceDemo
every ArithmeticException still carried a stack trace in 300000 throws
+13
View File
@@ -0,0 +1,13 @@
Benchmark (depth) Mode Cnt Score Error Units
ExceptionCostBenchmark.noStackTrace 1 avgt 5 9.810 ± 2.142 ns/op
ExceptionCostBenchmark.noStackTrace 10 avgt 5 345.467 ± 24.683 ns/op
ExceptionCostBenchmark.noStackTrace 50 avgt 5 1610.131 ± 148.390 ns/op
ExceptionCostBenchmark.normalException 1 avgt 5 989.076 ± 239.352 ns/op
ExceptionCostBenchmark.normalException 10 avgt 5 1589.303 ± 47.103 ns/op
ExceptionCostBenchmark.normalException 50 avgt 5 4137.934 ± 462.310 ns/op
ExceptionCostBenchmark.preallocated 1 avgt 5 3.240 ± 0.190 ns/op
ExceptionCostBenchmark.preallocated 10 avgt 5 336.605 ± 33.790 ns/op
ExceptionCostBenchmark.preallocated 50 avgt 5 1578.591 ± 74.969 ns/op
ExceptionCostBenchmark.returnCode 1 avgt 5 1.101 ± 0.033 ns/op
ExceptionCostBenchmark.returnCode 10 avgt 5 10.786 ± 0.301 ns/op
ExceptionCostBenchmark.returnCode 50 avgt 5 52.932 ± 1.374 ns/op
+2
View File
@@ -0,0 +1,2 @@
frames the program actually descended: more than 1024 (true)
getStackTrace().length: 1024
+4
View File
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.ankurm.exceptions.ExceptionBehaviourTest
-------------------------------------------------------------------------------
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.105 s -- in com.ankurm.exceptions.ExceptionBehaviourTest
+3
View File
@@ -0,0 +1,3 @@
int MaxJavaStackTraceDepth = 1024 {product} {default}
bool OmitStackTraceInFastThrow = true {product} {default}
bool StackTraceInThrowable = true {product} {default}