Why are try blocks expensive?

前端 未结 11 2457
醉酒成梦
醉酒成梦 2020-12-02 13:13

I\'ve heard the advice that you should avoid try catch blocks if possible since they\'re expensive.

My question is specifically about the .NET platform: Why are try

11条回答
  •  忘掉有多难
    2020-12-02 13:41

    Slightly O/T, but...

    There is fairly good design concept that says you should never require exception handling. This means simply that you should be able to query any object for any conditions that might throw an exception before that exception would be thrown.

    Like being able to say "writable()" before "write()", stuff like that.

    It's a decent idea, and if used, it makes checked exceptions in Java look kind stupid--I mean, checking for a condition and right after that, being forced to still write a try/catch for the same condition?

    It's a pretty good pattern, but checked exceptions can be enforced by the compiler, these checks can't. Also not all libraries are made using this design pattern--it's just something to keep in mind when you are thinking about exceptions.

提交回复
热议问题