Reputation: 20274
I have the following case:
try{
auto result = do_something(some_input); //do_something() may throw exceptions
if (result == bad){
do_something_else(some_input);
}
}
catch(...){
do_something_else(some_input);
}
Some explanation:
do_something()
returns bad
if the process succeeded but the accuracy of process was not that good (It Computer-Vision process).do_something()
throw exception if it could not process the data due to some missing information or very bad input.do_something_else()
.My questions:
Theoretical code:
auto result = do_something(some_input); //do_something() may throw exceptions
if (result == bad OR exception was thrown){
do_something_else(some_input);
}
Upvotes: 0
Views: 76
Reputation: 217478
You may wrap the method:
auto try_do_something(SomeInputType& someinput) noexcept
{
try {
return do_something(some_input); //do_something() may throw exceptions
} catch (...) {
// extra logging ?
return bad;
}
}
and then
if (try_do_something(some_input) == bad) {
do_something_else(some_input);
}
Upvotes: 1
Reputation: 2917
I suggest something like this
bool suceeded = false;
try
{
auto result = do_something
if (result != bad)
{
succeeded = true;
}
}
catch (specific_exceptions)
{
// Log or do something useful here
}
if (!succeeded)
{
do_something_else
}
else
{
use result
}
This attempts to keep the natural flow of your application out of exceptional cases, and to handle the two failures in their own distinct fashion while still funneling them to a single continuing case.
Upvotes: 2
Reputation: 4874
Personally, I'd go with something like:
try {
auto result = do_something(some_input);
if (result == bad) throw BadResultException();
} catch (...) {
do_something_else(some_input);
}
Naturally, this requires you to define your own exception types, but that's reasonably trivial.
To strictly answer your question, though, no, what you're asking isn't really possible as it stands because of how exceptions work. You need to do something like this (or your proposed solution) instead.
Upvotes: 0