The following new example code in the Throwable javadoc for throwing a
"legacy throwable" with a cause:
* <tt>Throwable</tt>. For example:
* <pre>
* try {
* lowLevelOp();
* } catch LowLevelException(le) {
* throw new HighLevelException().initCause(le); // Legacy constructor
* }
* </pre>
will very rarely be directly mimic-able in practice, because it will
only compile if this try block is within another try block that catches
Throwable or a method that declares that it throws Throwable.
So just to avoid people making the same mistake that I just did by
blindly mimicking the example code and getting the obvious compiler
error, it might be useful to augment the example code with the
appropriate cast:
throw (HighLevelException) new HighLevelException().initCause(le);
or:
HighLevelException he = new HighLevelException();
he.initCause(le);
throw he;
"legacy throwable" with a cause:
* <tt>Throwable</tt>. For example:
* <pre>
* try {
* lowLevelOp();
* } catch LowLevelException(le) {
* throw new HighLevelException().initCause(le); // Legacy constructor
* }
* </pre>
will very rarely be directly mimic-able in practice, because it will
only compile if this try block is within another try block that catches
Throwable or a method that declares that it throws Throwable.
So just to avoid people making the same mistake that I just did by
blindly mimicking the example code and getting the obvious compiler
error, it might be useful to augment the example code with the
appropriate cast:
throw (HighLevelException) new HighLevelException().initCause(le);
or:
HighLevelException he = new HighLevelException();
he.initCause(le);
throw he;