Using java, I want to do something like
try {
someMethod();
} catch (Throwable x) {
x.setMessage("Some comment " + x.getMessage());
throw x;
}
That is, I do not know what "someMethod" will throw. Whatever it is, I want to add a message at the beginning of its message, and then throw the error. But Throwable does not have a setMessage() method.
I can do a Class<? extends Throwable> cls = x.getClass(); to get the class type, but I am not sure of the syntax. I can't do
a throw new cls("Comment " + x.getMessage()); I am sure there must be a fairly simple way to do this, when you don't know the class of the throwable that is thrown.
catch (Throwable t) { throw new CustomExceptionOrSomething("Some comment", e); }?Throwable, since those could be JVM-level conditions (e.g.,OutOfMemoryError). In almost all cases, you'd be better off catchingExceptioninstead.