I have several dozen objects exposed through COM interfaces, each of which with many methods, totaling a few hundred methods. These interfaces expose business objects from my app to a scripting engine.
I have been given the task of protecting every single one of these methods from exceptions being thrown (to catch them and return an error using COM’s Error() function, which incidentally I can find no documentation on because it’s impossible to google). To my understanding, this requires that I add a try/catch around the guts of each one of these methods. The catch blocks are going to be similar or identical for each and every one of these hundreds of methods, which strongly smells of a problem (massively violates the DRY principle), but I can’t think of any way to avoid changing every method. As far as I can tell, these methods are invoked directly by COM, with no intervening code that I can hook into to catch the exceptions. My current best idea is to make a macro for the catch block, but that has it’s own sort of code-smell. Can anyone come up with a better approach?
BTW, my app’s exceptions do not derive from std::exception, so if there is some way of COM automatically handling standard exceptions, it won’t help. And I sadly cannot change the existing exceptions to derive from std::exception.
The most reliable C++ way is to use macros here. I’ ready to accept downvotes for saying this, but we’ve been using this solution for years and haven’t seen any serious problems so far.
Define a “begin method” macro for clearing
IErrorInfoandtry {and “end method” for} catchand the error handling. If you design the macros right – put all except the most necessary error handling code into helper functions it will be a tolerable and reliable solution with clean-looking moderately maintainable code.Yes, that doesn’t look good at all, but at least it is reliable and standard-compliant way of achieving what you want.