'Generic and Specific exception handling

Are there any performance improvement in specific exception handling over generic one (in any language)?.

I understand the implication of both in terms of good programming practice, ie, Specific exception handling triumphs over Generic one . But here, I am strictly asking from a performance view point.

  1. Will the application be consuming more memory figuring out which exception has been thrown as in the case of Generic Exception handling or,

  2. The memory/performance will be affected by having to go through multiple catch statements as in specific exception handling ?



Solution 1:[1]

In c# at least, one of the major performance penalties occur when you throw the exception, since it needs to gather data about the call stack. There is to my knowledge little to no performance or memory effect of catching more specific exceptions.

But if you are worried about the performance penalty you are not using exceptions correctly. They are intended for exceptional circumstances, i.e. they should be rare. If you have any measurable performance impact from exceptions you probably want to use some other pattern, like returning a result object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JonasH