Why is CLion not showing exceptions? - c++

CLion appears to be not showing me any exceptions when running my code. To test this, I've created a new project with only the following code:
#include <iostream>
int main() {
std::cout << "--- One" << std::endl;
throw 6;
std::cout << "--- Two" << std::endl;
return 0;
}
Which leads to the following output:
C:\Users\david\CLionProjects\untitled\cmake-build-debug\untitled.exe
--- One
Process finished with exit code 0
As you can see, code before the exception is executed and code following it is not executed (as you would expect). But instead of a message about the exception, it says "Process finished with exit code 0", as if no exception had occurred.
The same code compiled and executed on Ubuntu (via terminal) displayed an error message. So I assume the problem is with CLion.
How can I resolve this problem so that I can see messages for exceptions in my code?
Is there any setting that could lead to such behaviour?
I'm using CLion on Windows 10 with Cygwin. Here's a screenshot of the problem:

Throw requires also try and catch
From:
http://www.cplusplus.com/doc/tutorial/exceptions/
// exceptions
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
that compiled and run under cygwin shows:
$ g++ prova1.cc -o prova1
$ ./prova1
An exception occurred. Exception Nr. 2

Related

Unhandled exception "terminate called after throwing an instance of" overwrites last line of console

If you throw an unhandled exception out of main like this:
#include <stdexcept>
#include <iostream>
int main()
{
std::cout << "Hello World 1" << std::endl;
throw new std::invalid_argument("A");
return 0;
}
... then the process will terminate with the message "terminate called after throwing an instance of 'std::invalid_argument*'".
You will actually see this on the console:
Hello World 1
terminate called after throwing an instance of 'std::invalid_argument*'
If you print some more text without std::endl and then throw the exception:
#include <stdexcept>
#include <iostream>
int main()
{
std::cout << "Hello World 1" << std::endl;
std::cout << "Hello World 2";
throw new std::invalid_argument("A");
return 0;
}
... then you won't see the second line on the console:
Hello World 1
terminate called after throwing an instance of 'std::invalid_argument*'
It seems that this error message overwrites the last line, e.g. by printing \r before it.
How can this behavior be fixed?
The second line is not actually overwritten by the message, it never reaches the console!
The problem is that std::cout buffers its output and that this buffer is not flushed if the process terminates. (The error message is actually printed to standard error, not standard output.) And that problem was already discovered:
Is it possible to make std::cout flush automatically before program termination in various failure cases (e.g., exception)
In my case I used this to forcefully flush the buffer:
std::terminate_handler oldTerminateHandler;
void newTerminateHandler()
{
std::cout << std::endl;
if (oldTerminateHandler != nullptr)
{
oldTerminateHandler();
}
}
int main()
{
oldTerminateHandler = std::set_terminate(&newTerminateHandler);
//...
}

C++ promise.set_value fails with unknown error under linux

I'm trying to get my simulation running on our high performance server. It (unfortunately) uses CentOS Linux release 7.7.1908 (Core) instead of Win10, under which I'm developing the program. With this came a large amount of errors, one of them I was not able to fix on my on:
#include <future>
#include <iostream>
int main(int argument_count, char** arguments) {
int i = 1234;
std::cout << "Initialized i" << std::endl;
std::promise<int> promise;
std::cout << "Constructed promise" << std::endl;
promise.set_value(std::move(i));
std::cout << "Set value" << std::endl;
std::future<int> future = std::move(promise.get_future());
std::cout << "Retrieved future" << std::endl;
int j = std::move(future.get());
std::cout << "Got value: " << j << std::endl;
return 0;
}
When compiling this under Win10 with "cl test.cpp", the output looks like I'd expect:
Desktop>test.exe
Initialized i
Constructed promise
Set value
Retrieved future
Got value: 1234
On the other hand, when compiling on the server with "g++ -std=c++11 test.cpp", the output is different:
~/test_dir$ ./a.out
Initialized i
Constructed promise
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted
I do get the same error when trying this with an Ubuntu 16.04.6 LTS machine. I don't see why this happens.
Obviously, something is fishy in this line: promise.set_value(std::move(i)) since the output before is printed and the line after that statement is not executed any more. Furthermore, the compiler/ linker does find a one of the two versions "void set_value (const T& val);" or "void set_value (T&& val);" that would be appropriate for the template specification "int", i strongly suspect the later.
But why is the program aborting when setting an integer as the value of a promise? Even when inlining the value and skipping the variable all together does produce the error.
Can someone point me to where the error is?
Try compiling using the pthread flag:
g++ -std=c++11 test.cpp -pthread
Linking against both pthread and stdc++ may resolve the issue.
Example adding stdc++ in Cmake:
target_link_libraries(
# Your library or binary here
stdc++
pthread
)
Tested with the following code:
#include <iostream>
#include <future>
int main(int argc, char *argv[]) {
static_cast<void>(argc);
static_cast<void>(argv);
std::promise<int> pr;
auto fut = pr.get_future();
pr.set_value(10);
std::cout << fut.get() << std::endl;
return 0;
}

bad_alloc preempted in VS 2010/2013 C++

This simple example works as expected in g++, but in MS VS 2010 or 2013 shows a runtime library debug error (Invalid allocation size) before the error is caught (clicking Ignore does then flow through the error handler showing it correctly to be a bad_alloc).
Any ideas about why VS behaves this way?
#include <iostream>
#include <exception>
using namespace std;
int main() {
int x;
cout << "Enter -1 for bad_alloc: ";
cin >> x;
try
{
int* myarray = new int[x];
}
catch (exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
return 0;
}
That's so that you can analyse the exception at the point it is thrown, before the stack is unwound.
It's a feature that the debugger does this for you.
Nothing to worry about here.

After catching exception, C++ program aborts on return

I've looked around and found some similar issues but my particular one is stumping me.
Here is a code fragment in a test program, this is in the main() function:
try {
// some code that throws an exception
} catch(myexception& err) {
std::cout << "Shouldn't have encounterd an error\n";
std::cout << err.what() << endl;
}
cout << "Returning\n";
return 0;
I'm catching the exception and printing the messages. Then I print the "Returning" message and return. On the return I'm getting
Returning
Aborted (core dumped)
The stackdump tells me nothing. I am building on Windows 7 under Cygwin using g++ 4.7.3, can someone clue me in as to what might be happening and how to trap it?
--->
Kudos to David Rodríguez. You were right, a destructor attempted to free some area of memory that was already free.

Command processor has stopped working

I was writing a code for exception handling on Visual C++ 2010 .Here is the code
#include <iostream>
using namespace std;
// Localize a try/catch to a function.
void Xhandler(int test)
{
try{
if(test) throw test;
}
catch(int i) {
cout << "Caught Exception #: " << i << '\n';
}
}
int main()
{
cout << "Start\n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "End";
return 0;
}
The Program executed properly and the output was the as expected.But when I pressed the close button for closing the console then an error came that cmd has stopped working
.Then I ran my previous code that executed properly ,they also gave the same error
.
Can anybody tell why it is happening?Is it a problem with the Visual c++ 2010 or the code
I think your problem is not with your code. The problem is within your compiler tool chain. You probably are using Qt, and the tool chain has a problem causing this. Google the message you get when you crash with your IDE.
Here's a simple experiment to prove what I'm saying: just run this code:
int main()
{
cout << "Start\n";
cout << "End";
return 0;
}
And your program will crash, which means you have no problems with exceptions or anything else in your code, but with your tool chain.