Saturday, 15 August 2015

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



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

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

i found stack overflow question/answer why seek 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 grab 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 create 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 seek block! exceptions thrown asynchronously, such calling stop on thread (which deprecated), , besides outofmemoryerror can happen anywhere) , yet can caught , code go on execute afterwards in same method, more hard reason optimizations can made, less happen. (someone have programme compiler them, reason , guarantee correctness, etc. it'd big pain meant 'exceptional') again, in practice won't notice things this.

java performance try-catch

No comments:

Post a Comment