Throwing an exception that isn't caught.

I'm in Visual Studio playing around with the following code.

When doStuff() throws 2, what is the vanilla way of handling the exception?
I thought the way it works is that it simply exits the program. In visual studio, however, I'm given two options.

1.) Break - which I think basically means exit the program
2.) Continue - which I think basically means just exit the try block.

So if I'm on linux, or not in debugging mode. Which one of the two will my program do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

void doStuff() {
	throw 2;
}

void doEnough() {
	int hi = 2;
}

int main() {
	try{
		doEnough();
		doStuff();
	}
	catch (char * message) {
		std::cout << message << std::endl;
	}


	std::cout << "Im a pirate.";
	char wait;
	std::cin >> wait;
	return 0;
}
Last edited on
That break/continue popup means Visual Studio encountered an unhandled exception.

You have an unhandled exception because you don't have a catch block matching the type of the exception thrown. Line 4, you're throwing an int. Line 16: you have a catch block that is expecting a const char *.

Add the following:
1
2
3
catch (int val)
    {   std::cout << "Exception: " << val;
    }

Or change line 4 to throw a C-string.
Topic archived. No new replies allowed.