Why are exceptions harmful to performance?

See this question where everyone talks about how “obvious” performance will suffer, or exceptions should be avoided when performance is a problem, etc.

But I did not see a good explanation: why throwing exceptions is bad for performance, everything in this matter seems to be taken for granted.

The reason I'm asking about this is because I am trying to optimize the application and noticed that several hundred exceptions are thrown and swallowed during certain actions, such as clicking a button to load a new page.

+5
source share
1 answer

Firstly, it’s just a bad design, because the “exception” has semantic meaning (“some circumstance prevented this method from fulfilling its contract”) and that the abuse of this function was an unpleasant surprise.

In the case of Java, creating exception objects (in particular, populating stack traces) is extremely expensive because it involves moving the stack, many object distributions, and string manipulation, etc. In fact, throw an exception not if the main penalty for execution.

+5
source

All Articles