Performance Considerations for throwing Exceptions

前端 未结 8 1110
旧时难觅i
旧时难觅i 2020-12-19 03:17

I have come across the following type of code many a times, and I wonder if this is a good practice (from Performance perspective) or not:

try
{
    ... // s         


        
8条回答
  •  Happy的楠姐
    2020-12-19 04:15

    The throw in your first example has the overhead of the creation of a new CustomException object.

    The re-throw in your second example will throw an exception of type Exception.

    The re-throw in your third example will throw an exception of the same type that was thrown by your "some code".

    So the second and third examples use less resources.

提交回复
热议问题