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?
In principle, this is the unwinding of the most expensive stack. Here's a great link: Why is block tracing expensive?
. , - (), catch.
catch
. :
:
if (foo.CanFizz) foo.Fizz();
try { foo.Fizz(); } catch (NotFizzableException) { /* etc. */ }
, , , foo.CanFizz , . , , .
foo.CanFizz
, - . , , .
- "- , ".
, , , , , .
"" , , , , ( , , ).
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.
Source: https://habr.com/ru/post/1745818/More articles:вместо кнопки - javascriptASP.Net Layered app - Совместная структура данных сущностей среди слоев - asp.nethttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1745815/is-the-entity-framework-4-unit-of-work-pattern-the-way-to-go-for-generic-repositories&usg=ALkJrhhzAm6TFmoSYpaBf2qAPjjlY0nL7wXSLT 1 Simple Text Spacing - formattingCreating a static HTML site from XML content - pythonA WordPress form creation plugin that allows you to: Step 1> Step 2> Step 3> Submit - wordpressHow to get the computer name (host name in the web application)? - c #Sending a file in API C # - c #Publication status via Facebook graph api - phpWhere to store web service exceptions? - c #All Articles