Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Handling vs performance [duplicate]

Tags:

c#

exception

I know that exceptions have a performance penalty, and that it's generally more efficient to try and avoid exceptions than to drop a big try/catch around everything -- but what about the try block itself? What's the cost of merely declaring a try/catch, even if it never throws an exception?

like image 661
Merus Avatar asked Apr 19 '26 12:04

Merus


1 Answers

The performance cost of try is very small. The major cost of exception handling is getting the stack trace and other metadata, and that's a cost that's not paid until you actually have to throw an exception.

But this will vary by language and implementation. Why not write a simple loop in C# and time it yourself?

like image 157
JSBձոգչ Avatar answered Apr 21 '26 00:04

JSBձոգչ