C # performance on errors

It seems that catching the error is slower than checking before the error (e.g. TryParse). The relevant questions that prompt this observation are here and here .

Can someone tell me why this is so - why is it more expensive to catch an error, to do one or more data checks to prevent the error?

+3
source share
5 answers

In principle, this is the unwinding of the most expensive stack. Here's a great link: Why is block tracing expensive?

+5
source

. , - (), catch.

. :

:

if (foo.CanFizz)
    foo.Fizz();

:

try { foo.Fizz(); }
catch (NotFizzableException) { /* etc. */ }

, , , foo.CanFizz , . , , .

, - . , , .

+3

- "- , ".

0

, , , , , .

0

"" , , , , ( , , ).

:

It doesn't matter that throwing / catching an exception is “slow” (and it really isn't that slow), because if you use exception handling code, you should be outside the expected normal flow of your program.

0
source

Source: https://habr.com/ru/post/1745818/


All Articles