In the try catch block is it bad practice to return values from the catch block in C++?
try
{
//Some code...
return 1;
}
catch(...)
{
return 0;
}
Which method of using try/catch is good practice?
No, As long as the returned value means what you wanted it to be, you can return anytime. (make sure you've cleared memory if allocated).
I prefer to have few exits points in my code, so I would write:
int rc = 1;
try {
// some code
}
catch(...) {
rc = 0;
}
return rc;
I find it easier to debug and read code when I only have to keep track of one return statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With