Exception handling
No. 211
Q: |
Consider the following snippet:
Facilitate the above code and explain why your simpler implementation always and under all circumstances produces an identical runtime result. Tip
|
A: |
Both ClassCastException and ArithmeticException are being derived from their common parent RuntimeException. All three classes share the common Throwable#getMessage() method by inheritance without redefining it. Our two
Caveat: There is a whole
bunch of other child classes of RuntimeException.
The simplified version thus potentially catches additional
exceptions we might not be aware of. On the other hand the |