Tag Info

New answers tagged

2

If you want an OCP-conforming way of handling an unspecified list of exceptions, then you can consider "chain of responsibility". But it's not clear from your question whether your code is specified to handle "an arbitrary set of exceptions, each in a different way, all of which will be specified in future and is subject to change", or whether the fixed list ...


1

I think Phil's answer should get accepted in the end because it contains the best info. But to just answer your Is there another way for doing this? question specifically: here is another way which seperates the exception handling part pretty well, though it depends on the exact situation how usefull it is template< class Fun > void TryCatch( Fun f ) ...


3

If you just want to log each exception - in some way depending on its specific type, you can split that concern out into a dedicated exception logger. First, you need a base type - let's assume you just subclass std::exception. Now, your main code can just be: try { // something that throws } catch (const std::exception& e) { ...


5

That depends on how you plan to recover from these exception, meaning what code is executed in the catch statements. If it's the same for all 3 exceptions, you could create a superclass for them and catch the superclass. If your method has to throw a new exception, just make it extend the superclass and it would still be caught by the caller and would not ...



Top 50 recent answers are included