Say I have
public class A {
public void myMethod() throws Exception {
}
}
public class B extends class A {
}
If I now have an instance of B
and call myMethod()
which throws an exception, the stacktrace does not contain B.myMethod()
because B
does not implement myMethod()
.
How can I somehow print the calling super class in the log?, even if it's not implementing the method.
The use case is if I have 1000 classes extending A and only one has an exception in myMethod()
I cannot see which one.
Btw, implementing in the method in B
, just calling super.myMethod()
is not an option because it would be too much effort in the existing code base. but maybe it's the only option?
One option would be to wrap and rethrow the exception in class A, but print a message showing the class of the calling object. It could look like this.
public class A {
public void myMethod() throws Exception {
try {
// Do some stuff
} catch (Exception e) {
System.out.println("Exception thrown by " + getClass());
throw e;
}
}
}
This works because getClass()
returns the class of the calling object, not the class that has the code where it's actually called from.