I have a class
public class MyException extends RuntimeException {
public MyException(String s) {
super(s);
}
}
And method
void fun() throws NoSuchFieldException, IllegalAccessException {
Exception ex = new MyException("For fun!");
Class<? extends Exception> cl = ex.getClass();
Field msg = cl.getField("detailMessage");
msg.setAccessible(true);
msg.set(ex, "Yes!");
msg.setAccessible(false);
System.out.println(ex.getMessage());
}
detailMessage
is a field in Throwable
class. I want to get a message, which has been setted in the child class. How can I do it?
Currently, I'm getting an error:
Exception in thread "main" java.lang.NoSuchFieldException: detailMessage
at java.lang.Class.getField(Class.java:1556)
at Test.fun(Test.java:12)
at Test.main(Test.java:5)
Aucun commentaire:
Enregistrer un commentaire