java - Is it expensive to use try-catch blocks even if an exception is never thrown? -


we know expensive catch exceptions. but, expensive use try-catch block in java if exception never thrown?

i found stack overflow question/answer why try blocks expensive?, .net.

try has no expense @ all. instead of doing work of setting try @ runtime, code's metadata structured @ compile time such when exception thrown, relatively expensive operation of walking stack , seeing if try blocks exist catch exception. layman's perspective, try may free. it's throwing exception costs - unless you're throwing hundreds or thousands of exceptions, still won't notice cost.


try has minor costs associated it. java cannot optimizations on code in try block otherwise do. example, java re-arrange instructions in method make run faster - java needs guarantee if exception thrown, method's execution observed though statements, written in source code, executed in order line.

because in try block exception can thrown (at line in try block! exceptions thrown asynchronously, such calling stop on thread (which deprecated), , besides outofmemoryerror can happen anywhere) , yet can caught , code continue execute afterwards in same method, more difficult reason optimizations can made, less happen. (someone have program compiler them, reason , guarantee correctness, etc. it'd big pain meant 'exceptional') again, in practice won't notice things this.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -