Overriding fillInStackTrace for a standard JVM Exceptions

梦想与她 提交于 2019-12-07 20:46:27

No, since getMethod() calls new directly and you can't replace the code for NoSuchMethodException since the class is signed and fillInStackTrace() is native.

Your best bet is to cache calls to getMethod() in a central place: Just create a two level map: Map<Class, Map<String, Method>> and use a quick lookup without any exception throwing.

My two following points don't exactly solution the title of your question, but I think they could be helpful...


I confirm your performance measure.

I read about a solution in a java performance book. We have applied this to our own application, for some exceptions (where the stack trace is not important, and the possible frequency is high). I don't know if you will like it ... ;-)

Create a unique instance of your Exception class, store it. Throw that instance.

This seems ideal when you don't want to disturb an existing flow that relies on exceptions.


If your compiler complains about the other method not throwing that exception, it is because you chose a checked Exception.
Use a subclass of RuntimeException (they are unchecked, so the compiler doesn't know if they are thrown or not, he won't complain).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!