c++ googletest handling exceptions within try-block [duplicate] - c++

This question already has answers here:
why does throw "nothing" causes program termination?
(7 answers)
Closed 6 years ago.
When I start my gtest project with a structure like this, the code always breaks at the throw-statement, as if there is no try-catch around it.
Is There a way of changing the behaviour to just go on in the catch-block?
void errornousFunction()
{
try
{
int i = 5;
throw;
}
catch ( ... )
{
int i = 5;
}
}
TEST(testCaseName,asdf)
{
errornousFunction();
}

void errornousFunction()
{
try
{
int i = 5;
throw i;
}
catch ( ... )
{
int i = 5;
}
}
then u can try

Related

Bypasses initialisation in goto statement [duplicate]

This question already has answers here:
Goto before variable initialization causes compiler error
(4 answers)
initialization of 'unused' is skipped by 'goto label' - why do I get it for std::string but not for int?
(2 answers)
Closed 12 days ago.
I've come across an error that I thought should have been a warning:
char* p;
int main()
{
if (p) goto continue_func;
int a = 3;
continue_func:
int b = 2;
}
The error I get:
initialization of 'a' is skipped by 'goto continue_func'
transfer of control bypasses initialization of:
I don't see why this is illegal. If the case had been something like this:
char* p;
int main()
{
if (p) goto continue_func;
int a = 3;
continue_func:
int b = 2;
int c = a + b;
std::cout << c;
}
Then I understand that a is being used. But in the first example it's not. Is this truly illegal C++, and why?

How to return nothing from an integer function in C++? [duplicate]

This question already has answers here:
In a non-void function I want to return nothing
(2 answers)
When and how should I use exception handling?
(7 answers)
Closed 6 months ago.
Consider the following code:
#include <iostream>
int test(int a){
if (a > 10){
return a;
}
else{
std::cout << "Error!";
return nothing;
}
}
int main(){
std::cout << test(9);
return 0;
}
What I want is that The integer function test(int a), return a if a > 10, otherwise return Error!. but since this is an integer function, it must return an integer value, but I want that it print Error and return nothing. Is there a way for do this? (Also note that I don't want to use a void function)
#include <stdexcept>
int test(int a){
if (a > 10){
return a;
}
else{
throw std::invalid_argument( "a is smaller or eq than 10" );
}
}

C++ successful `try` branch

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.

Valgrind reporting "brk segment overflow in thread #1" [duplicate]

This question already has answers here:
Valgrind reporting a segment overflow
(5 answers)
Closed 6 years ago.
I wonder what this message implies:
==18151== brk segment overflow in thread #1: can't grow to 0x4a26000
Note that the code runs just fine and the output is correct. Should I just ignore this message? And what does it mean?
I think you can ignore it. I got the message in a new allocation in some code that seemed to work perfectly and I also get the message it in the following code:
#include <vector>
struct Something
{
Something() : a1(0), b1(0) { }
unsigned short a1;
unsigned short b1;
};
const int allocsize = 10000;
struct Tester
{
Tester()
{
for (int u = 0; u < allocsize; ++u)
data.push_back(new Something[519]);
}
~Tester()
{
for (int u = 0; u < allocsize; ++u)
delete[] (data[u]);
}
std::vector<Something*> data;
};
void test()
{
Tester t;
// while (true) {;}
}
int main()
{
test();
return 0;
}
I also noticed that others experience the same issue:
Valgrind reporting a segment overflow

Having a tough time understanding exception handling c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am having a very tough time understanding exception handling after watching online tutorials and reading up on it. I am trying to pass test driven development, and I can't. What I have come up with so far is this. I am supposed to use this struct
struct ArrayException
{
ArrayException(string newMessage = "error") :message(newMessage)
{
}
string message;
};
The first try.
int sum(int* theArray, unsigned int arraySize)
{
try
{
if (theArray = NULL)
{
throw ArrayException("NULL ARRAY REFERENCE");
}
}
catch (int* param)
{
cout << "you can't have " << param << " as an array size";
}
int sum = 0;
for (int i = 1; i < arraySize; i++)
{
sum += theArray[i];
}
return sum;
}
I also tried doing it this way.
int sum(int* theArray, unsigned int arraySize)
{
if (theArray = NULL)
{
throw ArrayException("NULL ARRAY REFERENCE");
}
else
{
int sum = 0;
for (int i = 1; i < arraySize; i++)
{
sum += theArray[i];
}
return sum;
}
}
While the post does not specifically mention it, I take it that the question is why exception is not caught? The answer is simple - because exception thrown is of type ArrayException, and catch is done with the type int*.
The best way to get a grip on this stuff is as πάντα ῥεῖ recommended: get a good book. Here's a good place to start selecting books: The Definitive C++ Book Guide and List
The rest is a code block with comments where I figured they were needed.
#include <iostream>
// removed the using namespace std;
struct ArrayException
{
ArrayException(std::string newMessage = "error") :
message(newMessage)
{
}
std::string message;
};
int sum(int* theArray, size_t arraySize) // change made here:
// size_t better suited than unsigned int for indexes
{
//throw std::out_of_range("BOOM!"); //uncomment to trigger a std::exception
//throw 42; // uncomment to trigger the unknown exception
if (theArray == NULL)
{
throw ArrayException("NULL ARRAY REFERENCE"); //perfect!
}
else
{
int sum = 0;
for (size_t i = 0; i < arraySize; i++) // changes made here:
// size_t not int to get rid of signed/unsigned conflict
// starting with array index 0, not 1
{
sum += theArray[i];
}
return sum;
}
}
int main()
{
try
{
sum (NULL, 10); // NULL address to force the exception
}
catch (ArrayException & param) // catch the custom exception
// catch by reference where possible
{
std::cout << "This bad stuff happened: " << param.message << std::endl;
}
// extra stuff to show what can also be done
catch (std::exception & stdexcpt) // catch any standard exceptions and
// print their message
{
std::cout << "Standard exception thrown: " << stdexcpt.what() << std::endl;
}
catch (...) // catch anything else that was thrown and doesn't
// correspond to any expectation
{
std::cout << "No idea what happened." << std::endl;
}
}