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.
Related
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
I am new to C++, and now learning it using code blocks (version: codeblocks-16.01mingw-setup.exe). My test codes are as follows:
#include<iostream>
#include<stdlib.h>
int main()
{
int sum = 0, val = 1;
// keep executing the until val is greater than 10
while (val <=10 ) {
sum += val; // short-cut assignment
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
system("pause");
return 0;
}
These codes are written in an empty file named ex1.cpp. Then I tested by click "Build and run". As a result, another file main.cpp (I did not write this) pops up:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Screenshot attached for your better checking:
The reason why you are getting this error is because your compiler settings is not correct.You need to make sure that you use GNU GCC MinGW Compailer.Go To Settings-->Compiler and make sure every thing is same as on the screen shot.
Solving common codeblocks problems :Link
I really did something wrong about coding:
when I create an empty file in the project, it will result in two main functions in the that project one of which is that "hello world" file automatically generated, which is not allowed by C++.
To build it successfully, what I did is to overwrite the codes in the main.cpp.
I am starting with C++ (Visual Studio 2015 and Windows 8.1), with this simple code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
return 0;
}
But, the output screen shows nothing!, what shall I do?
Thanks in advance.
In Visual Studio, start the program with Ctrl-F5 and it will run and pause automagically for you. No additional code needed.
Your code is perfectly fine but the program currently only prints and exits right after, because this can happen very fast you might not be able to even see it,try pausing it :
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
cin.get();
return 0;
}
Also, make sure your Anti Virus isn't blocking Visual Studio.
Your code is just fine, however, if you execute it as a cmd program, the program window will close immediately, you might not be able to even see the output. You can write extra code to solve this problem by "pausing" the program:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "Hello world" << endl;
system("PAUSE");
return 0;
}
if you don't like include a windows.h file every time you type, you can add a "cin.get();" in the end of the code. But to be honest, since you are just a beginner, the coolest way I think you should try, is not to use Visual Studio to learn C/C++ but to install CodeBlocks(a simple but effective IDE) to write some codes that are not so long. You know, VS is for huge and complex projects and some practical program developing.
Another solution, platform dependent. My answer is for those of you who just need test pause for debugging purposes. It's not recommended release solution!
windows
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("pause");
return 0;
}
linux (and many alternatives)
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("read -rsp $'Press enter to continue...\n'");
return 0;
}
Detecting paltform
I used to do this on programming homework assignments, ensuring this only happens on windows:
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
#ifdef _WIN32
system("pause");
return 0;
}
Here's a good cheatsheet for ifdef macros and operating systems: http://sourceforge.net/p/predef/wiki/OperatingSystems/
The program exits on return 0; and window closes. Before this, you must pause the program. E.g you can wait for an input.
Here is a snippet from my code to do this. It works in both windows and linux.
#include <iostream>
using std::cout;
using std::cin;
// Clear and pause methods
#ifdef _WIN32
// For windows
void waitForAnyKey() {
system("pause");
}
#elif __linux__
// For linux
void waitForAnyKey() {
cout << "Press any key to continue...";
system("read -s -N 1"); // Continues when pressed a key like windows
}
#endif
int main() {
cout << "Hello World!\n";
waitForAnyKey();
return 0;
}
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.
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.