E
E
ebroker2019-03-03 17:40:02
.NET
ebroker, 2019-03-03 17:40:02

C# exception handling cost?

Following up on the C# question , what is the cost of type casting?
I met opinions that exception handling in C # slows down the application. Is it very noticeable in a real application?
Example in ASP NET Core

[HttpPut]
public IActionResult Update(SomeDto someDto)
{
     Validate(someDto);
     var result = DoBuissness(someDto);
     return new OkObjectResult(result);
}

What if the Validate method, when it finds an error during validation, will throw an exception throw new MyValidationEcxeption(); which will be handled by the global exception handler in ASP NET Core and will return the desired 400 status code for this request. The same with the DoBuissness method.
The second option is the classic implementation
[HttpPut]
public IActionResult Update(SomeDto someDto)
{
     var validationResult = Validate(someDto);
     if(string.IsNullOrEmpty(validationResult ))
         return new BadObjectResult(validationResult);
     var result = DoBuissness(someDto);
     return new OkObjectResult(result);
}

How adequate is the first scheme, in contrast to the classical second?
Interested primarily in speed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2019-03-03
@GavriKos

GENERATION of exceptions is not expensive. Therefore, the first and second pieces of +- code are comparable in speed.
Exception handling is expensive. There is a stack reversal, all sorts of shit ... For the processor - this is exactly the EXCEPTIONAL situation - there is no talk of any performance there - it's about maintaining efficiency.
But it is impossible to compare, because it is not clear what is there and how it is processed in the second fragment.

#
#, 2019-03-04
@mindtester

in the general case, it all comes down to the harmony of the error handling logic. maybe you should read this article
https://habr.com/ru/post/339606/
the author has a number of publications, including he writes quite actively about C#
https://habr.com/ru/users/marshinov/posts/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question