c++ - How to wrap calls with try/catch block? -


suppose, have different functions, can throw exceptions:

const foo& func_foo(...);  // can throw exceptions const bar& func_bar(...);  // can throw exceptions const foobar& func_foobar(...); // can throw exceptions 

i have different places in code, can use such functions in following way:

some_func(/*applying of func_foo or func_bar or func_foobar*/(...))  

actually, using result of functions in many places within different functions.

what best way wrap calling of func_foo/func_bar_func_foobar functions try/catch block without global rewriting of other pieces of code?

ideally want use (for example call func_foo)

some_func(try_catch_block(func_foo(...))); 

catch handler propagate exception different types

catch (const exceptionfoo& e) {    throw someexception1("some message e"); } catch (const exceptionbar& e) {    throw someexception2("some message e"); } 

i must admit find combining lambdas , macros quite fun.

#define try_catch_block(...)         \     [&]() -> decltype(auto) {        \         try {                        \             return __va_args__;      \         } catch(/* ... */) {         \             /* handle , rethrow */ \         }                            \     }() 

this can called specified, including interleaved inside function call.

some_func(try_catch_block(func_foo(...))); 

see live on coliru


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -