What are the effects of exceptions on performance in Java?

前端 未结 17 2372
情深已故
情深已故 2020-11-22 01:21

Question: Is exception handling in Java actually slow?

Conventional wisdom, as well as a lot of Google results, says that exceptional logic shouldn\'t be used for n

17条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 01:22

    My opinion about Exception speed versus checking data programmatically.

    Many classes had String to value converter (scanner / parser), respected and well-known libraries too ;)

    usually has form

    class Example {
    public static Example Parse(String input) throws AnyRuntimeParsigException
    ...
    }
    

    exception name is only example, usually is unchecked (runtime), so throws declaration is only my picture

    sometimes exist second form:

    public static Example Parse(String input, Example defaultValue)
    

    never throwing

    When the second ins't available (or programmer read too less docs and use only first), write such code with regular expression. Regular expression are cool, politically correct etc:

    Xxxxx.regex(".....pattern", src);
    if(ImTotallySure)
    {
      Example v = Example.Parse(src);
    }
    

    with this code programmers hasn't cost of exceptions. BUT HAS comparable very HIGH cost of regular expressions ALWAYS versus small cost of exception sometimes.

    I use almost always in such context

    try { parse } catch(ParsingException ) // concrete exception from javadoc
    {
    }
    

    without analysing stacktrace etc, I believe after lectures of Yours quite speed.

    Do not be afraid Exceptions

提交回复
热议问题