Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

java - Why is Throwable.fillInStackTrace() method public? Why would someone use it?

I'm curious why is method fillInStackTrace of java.lang.Throwable public?

This method replaces original stack trace with that from the place it is called, removing the information needed to localize exception. It could be used for obfuscating, but without much effort, since new stack trace would direct to the obfuscation code. Better way would be to simply hide the exception or throw the new one.

But I can't find out any reasonable case for calling this method on existing Throwable. So the question is: why this method is public? Is there any sense behind?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One reason is for performance. Throwing and catching an exception is cheap; the expensive part is filling in the stack trace. If you override fillInStackTrace() to do nothing, creating an exception also becomes cheap.

With cheap exceptions, you can use exceptions for flow control, which can make the code more readable in certain situations; you can use them when when implementing JVM languages where you need more advanced flow control, and they are useful if you are writing an actors library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...