I'm wondering how should one handle "contained" exceptions. Because the term isn't concrete enough, let me take an example of PrivilegedActionException
.
In short, this exception will in its cause
contain any checked exception thrown during the computation within PrivilegedAction
.
Now if I have and method computate() throws IOException
I'll - if executed on its own - handle it as:
try {
computate();
} catch (FileNotFoundException ex) {
// Handle file not found
} catch (SomeOtherSubtypeOfIOException ex) {
// handle that again
}
Now as this has been executed in PriviledgedAction
the only exception I get is PrivilegedActionException
:
try {
Subject.doAs(() -> computate());
} catch (PrivilegedActionException ex) {
// Now what?
}
I can get the IOException
from previous example by calling ex.getCause()
but how would that look like? The obvious way looks odd ...
catch (PrivilegedActionException ex) {
if (ex.getCause() instanceof FileNotFoundException.class) {
// handle FileNotFound
} else if (ex.getCause() instanceof xxx) {
// something else
}
}