Wrap every method in try-catch or specific part of code

前端 未结 5 960
星月不相逢
星月不相逢 2020-12-16 23:46

My senior colleague tells me to wrap every method within a try-catch block so they can trace where exceptions occurs to help debug issues quicker. Is it better to wrap every

5条回答
  •  忘掉有多难
    2020-12-17 00:25

    There is no problem in using Try catch block as it has no overhead (unless if you keep adding it everywhere which might include overhead in readability) while there is often additional IL added for catch and finally blocks,when no exception is thrown there is little difference in behaviour.

    However if your code does throw an exception this is where you have a slight performance issue as in such cases an exception must be created,stack crawl marks must be placed and, if the exception is handled and its StackTrace property accessed, a stack walk is incurred. so as a result it might not be good idea to always wrap your code in try catch block alternatively you can place it at a parent level and then inspect the stack trace

提交回复
热议问题