C++ successful `try` branch - c++

Let's consider some artificial C++ code:
int i = 0;
try { someAction(); }
catch(SomeException &e) { i = -1; }
i = 1;
... // code that uses i
I want this code to assign -1 to i in case of someAction() throws exception and assign 1 in case if there was no exception. As you can see now this code is wrong because i finally always becomes 1. Sure, we can make some trick workarounds like:
int i = 0;
bool throwed = false;
try { someAction(); }
catch(SomeException &e) { throwed = true; }
i = throwed ? -1 : 1;
... // code that uses i
My question is: is there anything in C++ like "successful try branch", where I do some actions in case if there were no any throws in try block?
Something like:
int i = 0;
try { someAction(); }
catch(SomeException &e) { i = -1; }
nocatch { i = 1; }
... // code that uses i
Surely, there is no nocatch in C++ but maybe there is some common beautiful workaround?

int i = 0;
try { someAction(); i = 1; }
catch(SomeException &e) { i = -1; }

Aside from the simple solution
try
{
someAction();
i = 1;
}
catch(SomeException &e)
{
i = -1;
}
you should consider what you are planning to do with the value of i further in the code - use it in if statements? That is poor design, you could simply put all the code inside the braces after try and catch respectively.

Related

Can this get to the end of the function and not return anything?

I am making a c++ text based game and while making a feature for eating food I made this function names eating. I am getting a warning saying its possible to reach the end with no return value. How is it possible to get to the end and not return something?
int inventory::eat(std::string eating)
{
if (!consumables.empty())
{
for (int i = 0; i < consumables.size(); i++)
{
if (consumables[i].name == eating)
{
return consumables[i].effect;
}
else
{
return 404;
}
}
}
else
{
return 505;
}
}
Presumably the compiler is unable to tell that if consumables.empty() == false, then consumables.size() > 0.
I'd probably rewrite it as this, but I'm worried about the if/else inside your for loop.
int inventory::eat(std::string eating) {
for (int i = 0; i < consumables.size(); i++) {
if (consumables[i].name == eating) {
return consumables[i].effect;
} else {
return 404;
}
}
return 505;
}
Assuming no other thread modifies consumables while eat() is running, then no, return will not be skipped. But the compiler doesn't know that. It doesn't knower that !empty() and size() > 0 mean the same thing for a container. They are just two separate method calls.
That said, I would suggest writing the code more like this, which is easier to read, and avoids the warning:
int inventory::eat(const std::string &eating)
{
if (consumables.empty())
return 505;
for (int i = 0; i < consumables.size(); ++i)
{
if (consumables[i].name == eating)
return consumables[i].effect;
}
return 404;
}
Or, if you are using C++11 and consumables supports iterators:
#include <algorithm>
int inventory::eat(const std::string &eating)
{
if (consumables.empty())
return 505;
auto found = std::find_if(
consumables.begin(), consumables.end(),
// replace 'consumable' below with your actual type name as needed...
[&](const consumable &c){ return c.name == eating; }
);
if (found != consumables.end())
return found->effect;
return 404;
}

Why is this for loop not correct?

Visual Studio is telling me that this for loop isn't correct.
Error Messages are:
type bool unexpected
ok is undeclared identifier
missing ; before }
infos:
-recordset.Select return a long -MoveNext a bool
for (size_t i = 0, bool ok = recordset.Select(Adress::getSQLStatement() + "Where A05.recid = %ld", i); ok; ok = recordset.MoveNext(), i++) {
at(i).Save(recordset);
}
It's as StenSoft said. But you can define an anonymous structure in the loops first statement, and initialize that.
#include <iostream>
using namespace std;
int main() {
for (struct {size_t i; bool ok;} s = {0, true}; s.ok; ++s.i) {
s.ok = s.i < 10;
cout << s.i;
}
return 0;
}
But IMHO, while it works, it's more trouble than it's worth. Better restructure you code.
First off, you can of course rewrite your loop like so:
{
bool ok = recordset.Select(...);
for (std::size_t i = 0; ok; ok = recordset.MoveNext(), ++i)
{
/* ... */
}
}
But the meta lesson here is that almost all loops are for-loops, and when you think your structure is different, think again. There's probably a rewrite in terms of for-loops that makes your logic clearer. Your present code doesn't distinguish an initial error from a "no more records" error later. With the new code, that's now explicitly possible:
if (bool ok = recordset.select(...))
{
for (std::size_t i = 0; ok; ok = recordset.MoveNext(), ++i) { /* ... */ }
}
else
{
// handle initial error
}
I would probably even get rid of the redundant ok variable:
if (recordset.select(...))
{
for (std::size_t i = 0; ; ++i)
{
/* ... */
if (!recordset.MoveNext()) break;
}
}
else
{
// handle initial error
}

With Do Begin Statement in C++ Builder

I wanna know how to write the following codes in C++ Builder by using With Do Begin statement similar to Delphi.
I tried with ComboBox->Text .... do ... try and it's not working. I tried with just do ComboBox->Text .... try, also not working.
if (ComboBox->Text.operator==(String("C++ Builder XE7")))
{
try
{
// do something
if ((Form1->Memo1->Lines->Text).Pos("<") !=0)
{
// do something
}
}
catch(Exception &ex)
{
ShowMessage(ex.ToString());
}
if (ComboBox->Text.operator==(String("C++ Builder XE8")))
{
try
{
// do something
if ((Form1->Memo1->Lines->Text).Pos("<") !=0)
{
// do something
}
}
catch(Exception &ex)
{
ShowMessage(ex.ToString());
}
There is no equivalent to Delphi's with statement in C++. The best you can do in C++ is use pointers/references instead, eg:
TComboBox *cb = ComboBox;
TStrings *lines = Form1->Memo1->Lines;
if (cb->Text == "C++ Builder XE7")
{
try
{
// do something
if (lines->Text.Pos("<") != 0)
{
// do something
}
}
catch(const Exception &ex)
{
ShowMessage(const_cast<Exception&>(ex).ToString());
}
}
if (cb->Text == "C++ Builder XE8")
{
try
{
// do something
if (lines->Text.Pos("<") != 0)
{
// do something
}
}
catch(const Exception &ex)
{
ShowMessage(const_cast<Exception&>(ex).ToString());
}
}

Error checking on many function calls

Sometimes when I am programming in C++/C I end up calling the same function multiple times and I was wondering what is the most efficient way to check for errors for all of those calls? Using if else statements take up a lot of code and look ugly. I have come up with my own way of checking for errors, perhaps there is a better way that I should use.
int errs[5] = {0};
errs[0] = functiona(...);
errs[1] = functiona(...);
...
errs[5] = functiona(...);
for (int i = 0; i < 5; i++)
{
if (err[i] == 0)
MAYDAY!_wehaveanerror();
}
Note: I understand that using try and catch might be better for C++ as it would solve this problem by throwing an exception on the first error, but the problem with that is that it is not compatible with a lot of functions that return error codes such as the Windows API. Thanks!
You could write some pseudo-C++ like this:
struct my_exception : public std::exception {
my_exception(int); /* ... */ };
int main()
{
try
{
int e;
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
}
catch (my_exception & e)
{
std::cerr << "Something went wrong: " << e.what() << "\n";
}
}
If...IF the function has a chance to throw a different error you should also add a catch all.
struct my_exception : public std::exception {
my_exception(int); /* ... */ };
int main()
{
try
{
int e;
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
}
catch (my_exception & e)
{
std::cerr << "Something went wrong: " << e.what() << "\n";
}
catch (...)
{
//Error Checking
}
}
What about handling the checking in a function?
void my_function() {
if (!create_window())
throw Error("Failed to create window");
}
int main() {
try {
my_function();
} catch (const Error& e) {
cout << e.msg << endl;
} catch (...) {
cout << "Unknown exception caught\n"
}
return 0;
}
If you're calling the same function over and over again, the most succinct way might be to use a macro. I would suggest something like:
#define CHECKERROR(x) if(x == 0) wehaveanerror()
CHECKERROR(function(...));
CHECKERROR(function(...));
Obviously, this macro would be very specific to the particular function and error handler involved, so it may be prudent to undef it after those calls.
Doing it more old-school, but keeping w/ the original error response but responding as soon as an error occurs w/o looking ugly:
#define callcheck(r) if ((r)==0) MAYDAY!_wehaveanerror()
callcheck(functiona(...));
callcheck(functiona(...));
...

How to implement final conditions properly?

This is what I'm trying to do (this is a simplification of a real project):
int param;
int result;
void isolated(int p) {
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
} catch (...) {
throw "problems..";
}
}
I can't change the way process() works, since this function is not created in the project and is a third-party function. It works with global variables param and result and we can't change this.
The problem appears when isolated() is called back from process() with another parameter. I want to catch this situation, but don't know how to do it, since finally is absent in C++. I feel that I should use RAII technique, but can't figure out how to do it in this case properly.
This is how I can make it with code duplication:
int param;
int result;
void isolated(int p) {
static bool running;
if (running) {
throw "you can't call isolated() from itself!";
}
running = true;
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
running = false;
} catch (...) {
running = false; // duplication!
throw "problems..";
}
}
"finally" like situations are handled in C++ using guard objects, that do their finally thing in the destructor. This is IMHO much more powerful approach, because you have to analyze the situation to finalize in order to create a reuseable object. In this case, we need to make process rentrant, because parameters and returns are passed in globals. The solution is to save their values on entry and restore them on exit:
template<class T>
class restorer
{
T &var; // this is the variable we want to save/restore
T old_value; // the old value
restorer(const restorer&);
void operator=(const restorer&);
public:
restorer(T &v) : var(v), old_value(v) {}
~restorer() { var=old_value; }
};
int param;
int result;
int isolated(int p) {
restorer<int> rest_param(param);
restorer<int> rest_result(result);
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
return result;
} catch (...) {
return 0;
}
}
Maybe I didn't get it right, but why don't you use a flag? You want to know when the isolated() is called from the process(), right?
int isolated(int p) {
static int execDeep = 0;
execDeep++;
// your code here
execDeep--;
}
Now you can check 'execDeep' value, > 1 means it is called from the process() while still being executed.
I still don't quite sure how finally is related here, but you could try Boost.ScopeExit if you want to avoid creating a scope guard structure yourself.
Example:
#include <boost/scope_exit.hpp>
#include <cstdio>
int isolated(int p) {
static bool running = false;
if (running) {
printf("Throwing %d\n", p);
throw p;
}
printf("Starting %d\n", p);
running = true;
BOOST_SCOPE_EXIT( (p)(&running) ) { // <--
printf("Stopping %d\n", p); // <--
running = false; // <--
} BOOST_SCOPE_EXIT_END // <--
// ...
if (p)
isolated(p*10);
// ...
printf("Returing %d\n", p);
return 4;
}
int main() {
printf(">> first\n");
isolated(0);
printf(">> second\n");
try {
isolated(1);
printf(">> third (should not be printed.)\n");
} catch(int p) {
printf("Caught %d\n", p);
}
isolated(0);
printf(">> fourth\n");
return 0;
}
Result:
>> first
Starting 0
Returing 0
Stopping 0
>> second
Starting 1
Throwing 10
Stopping 1
Caught 10
Starting 0
Returing 0
Stopping 0
>> fourth
Could this work?
int save = -10000000000;
int param;
int result;
int isolated(int p) {
if (save != -10000000000)
{
// run the other condition
}
else
{
save = p;
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
return result;
} catch (...) {
return 0;
}
}
}
If I understand correctly, you want to automatically set the running flag to false at the end of function. If that is the requirement then you can use the ScopeGuard approarch mentioned in the link.