Pure Programmer
Blue Matrix


Cluster Map

Exception Handling

L1

This page is under construction. Please come back later.

Many functions take in arguments, do a computation and return a result without any diffuculties. But some functions may run into problems that need to be reported to the calling code. Problems can be found in the arguments being passed, with system resources that are not available or with computations that just can't be performed. Returning an error code through the formal return value of a function is one technique but it requires the calling code to check the result of a function to see if the error code was returned. Often this step is omitted resulting in bad values being propogated through the code. A better approach is to have the function throw an exception. An exception is just an object that is returned through the alternate exceptional return mechanism instead of the normal return mechanism. In the following example the stoi() and stof() functions could fail if the string passed can't be converted to a number. In the case of stoi() this function returns numbers across the whole range of integers so there is no value that can be returned to indicate an error that is not also a valid integer. This is why these two functions return a value if they can and if they can not, they throw an exception. Because the code below doesn't check for execptions being thrown, it will simply quit and print an error message detailing the exception thrown if bad input is passed to either of the two functions.

Exceptions1.cpp
#include <fmt/format.h>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
	int i;
	double d;
	i = stoi(string(argv[1]));
	d = stod(string(argv[2]));
	cout << fmt::format("i + d = {0:f}", i + d) << endl;
	return 0;
}

Output
$ g++ -std=c++17 Exceptions1.cpp -o Exceptions1 -lfmt $ ./Exceptions1 1 3.1415926 i + d = 4.141593 $ g++ -std=c++17 Exceptions1.cpp -o Exceptions1 -lfmt $ ./Exceptions1 1 abc libc++abi: terminating due to uncaught exception of type std::invalid_argument: stod: no conversion $ g++ -std=c++17 Exceptions1.cpp -o Exceptions1 -lfmt $ ./Exceptions1 abc 3.1415926 libc++abi: terminating due to uncaught exception of type std::invalid_argument: stoi: no conversion

In the example below we add exception handling code around each of the two functions that can throw. This is done useing a try/catch construct. The try block wraps the code that might throw an exception. Following the try block is a catch block that contains the error handling code that is only executed if an exception is thrown in the preceding try block. When an exception is thrown, the try block is terminated immediately and control passes to the catch block. If no exception is thrown, all the code in the try block is executed and the catch block is skipped. The example below illustrates what will happen with good input and some bad input.

Exceptions2.cpp
#include <fmt/format.h>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
	int i = 0;
	double d = 0;
	try {
		i = stoi(string(argv[1]));
	} catch (exception ex) {
		cout << "Can't convert to integer!" << endl;
		exit(1);
	}
	try {
		d = stod(string(argv[2]));
	} catch (exception ex) {
		cout << "Can't convert to floating point!" << endl;
		exit(1);
	}
	cout << fmt::format("i + d = {0:f}", i + d) << endl;
	return 0;
}

Output
$ g++ -std=c++17 Exceptions2.cpp -o Exceptions2 -lfmt $ ./Exceptions2 1 3.1415926 i + d = 4.141593 $ g++ -std=c++17 Exceptions2.cpp -o Exceptions2 -lfmt $ ./Exceptions2 1 abc Can't convert to floating point! $ g++ -std=c++17 Exceptions2.cpp -o Exceptions2 -lfmt $ ./Exceptions2 abc 3.1415926 Can't convert to integer!

As this code illustrates, exception handling allows use to possibly recover from an error or at a minimum, present a more meaningful error message to the user and then terminate the program in an orderly fashion.

The example above illustrates one way to deal with exceptions, namely providing a try/catch construct to wrap each and every function that might throw. While this a very fine-grained approach that gives us lots of control, it can become tedious if many functions that can throw are being used. Another approach is presented below whereby all the core logic of our main code is wrapped in a single try/catch. If any of the functions in main throw an exception it will cause control flow to immediately jump into the catch handler. This approach requires less exception handling code but sometimes prevents us from knowing exactly which function had a problem that required it to throw an exception.

Exceptions3.cpp
#include <fmt/format.h>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
	int i = 0;
	double d = 0;
	try {
		i = stoi(string(argv[1]));
		d = stod(string(argv[2]));
		cout << fmt::format("i + d = {0:f}", i + d) << endl;
	} catch (exception ex) {
		cout << "Can't convert command line argument!" << endl;
	}
	return 0;
}

Output
$ g++ -std=c++17 Exceptions3.cpp -o Exceptions3 -lfmt $ ./Exceptions3 1 3.1415926 i + d = 4.141593 $ g++ -std=c++17 Exceptions3.cpp -o Exceptions3 -lfmt $ ./Exceptions3 1 abc Can't convert command line argument! $ g++ -std=c++17 Exceptions3.cpp -o Exceptions3 -lfmt $ ./Exceptions3 abc 3.1415926 Can't convert command line argument!
Exceptions4.cpp
#include <fmt/format.h>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
	int i = 0;
	double d = 0;
	try {
		i = stoi(string(argv[1]));
		d = stod(string(argv[2]));
		cout << fmt::format("i + d = {0:f}", i + d) << endl;
	} catch (invalid_argument ex) {
		cout << "Can't convert command line argument!" << endl;
	} catch (exception ex) {
		cout << "Don't know what went wrong!" << endl;
	}
	return 0;
}

Output
$ g++ -std=c++17 Exceptions4.cpp -o Exceptions4 -lfmt $ ./Exceptions4 1 3.1415926 i + d = 4.141593 $ g++ -std=c++17 Exceptions4.cpp -o Exceptions4 -lfmt $ ./Exceptions4 1 abc Can't convert command line argument! $ g++ -std=c++17 Exceptions4.cpp -o Exceptions4 -lfmt $ ./Exceptions4 abc 3.1415926 Can't convert command line argument!
Exceptions5.cpp
#include <fmt/format.h>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
	int i = 0;
	double d = 0;
	try {
		i = stoi(string(argv[1]));
		d = stod(string(argv[2]));
		cout << fmt::format("i + d = {0:f}", i + d) << endl;
	} catch (invalid_argument ex) {
		cout << "Can't convert command line argument!" << endl;
	} catch (exception ex) {
		cout << "Don't know what went wrong!" << endl;
	}
	// C++ doesn't support true finally blocks
	{
		cout << "This statement always executes." << endl;
	}
	return 0;
}

Output
$ g++ -std=c++17 Exceptions5.cpp -o Exceptions5 -lfmt $ ./Exceptions5 1 3.1415926 i + d = 4.141593 This statement always executes. $ g++ -std=c++17 Exceptions5.cpp -o Exceptions5 -lfmt $ ./Exceptions5 1 abc Can't convert command line argument! This statement always executes. $ g++ -std=c++17 Exceptions5.cpp -o Exceptions5 -lfmt $ ./Exceptions5 abc 3.1415926 Can't convert command line argument! This statement always executes.
cpp

Questions

Projects

More ★'s indicate higher difficulty level.

References