Exceptions

An Exception is an error or unpredictable behaviour (like run-time errors) within a program that is not caused by the OS.

 

Exception handling is the ability to deal with these problems, and allows the program to manage the problem rather than simply crashing.

 

The general technique is to use a try block and when an exception arises, it is thrown to the catch exception handler. If no exceptions are thrown, the code continues normally and all handlers are ignored.

 

Three keywords:

  • try { desired behaviour code block }
    • The normal behaviour is contained within the code block
  • catch ( parameters ) { code-to-execute}
    • The actual exception handlers
    • Always follows the try section, and must not have any code between the two
    • parameter(s) are passed in by the previous try section
    • ellipses can be used as a 'catch all' for non-defined data-types in the catch parameter. e.g. catch(...)
    • Deals with the problem. e.g. display error message
  • throw data-type-value
    • used to signal an error has occurred of the data-type-value specified
    • Typically this value is an error code, or description, or custom code

 

This example sends a value to a function, which then tests the value in the try block, then throws an exception according to the value test result, which is then caught by one of the catch blocks:

#include <iostream>
using namespace std;

void myExceptFunc(int test) {

	try {
		if (test==0)throw test;
		if (test==1)throw 'a';
		if (test==2)throw 1233.33;
	}

	catch(int i) {
		cout << "Exception caught of data type int, with a value of " << i << endl ;
	}

	catch(double n) {
		cout << "Exception caught of data type float, with a value of: " << n << endl ;
	}

	catch(...) {
		cout << "Exception caught using ellipses for default, non-specified data types!" << endl ;
	}
}

int main() {

	myExceptFunc(0); //send 0 by value to myExceptFunc()
	myExceptFunc(1); //send 1 by value to myExceptFunc()
	myExceptFunc(2); //send 2 by value to myExceptFunc()

	return 0;

}

Compile & Run:

Exception caught of data type int, with a value of 0
Exception caught using ellipses for default, non-specified data types!
Exception caught of data type float, with a value of: 1233.33

 

 

 

Another example:

#include <iostream>
using namespace std;

double divisor(double a, double b) {

	if (b == 0)
		throw "Cannot divide by zero!" ;

	return a / b ;
}

int main() {

	float x, y ;

	cout << "Please enter the first number to be divided: " << endl ;

	cin >> x ;

	cout << "Please enter the divisor: " << endl ;

	cin >> y ;

	try {
		cout << x << " divided by " << y << " equals: " << divisor(x, y) << endl ;
	}

	catch (const char *stringException) {
		cerr << "Error: " << stringException << endl ;
	}

	return 0;

}

Compile & Run:

Please enter the first number to be divided:
123.456
Please enter the divisor:
0
Error: Cannot divide by zero!

 

 

Standard exceptions defined in the #include <exception> header file. Note all are within the std namespace:

Exception Description
std::exception An exception and parent class of all the standard C++ exceptions.
std::bad_alloc This can be thrown by new.
std::bad_cast This can be thrown by dynamic_cast.
std::bad_exception This is useful device to handle unexpected exceptions in a C++ program
std::bad_typeid This can be thrown by typeid.
std::logic_error An exception that theoretically can be detected by reading the code.
std::domain_error This is an exception thrown when a mathematically invalid domain is used
std::invalid_argument This is thrown due to invalid arguments.
std::length_error This is thrown when a too big std::string is created
std::out_of_range This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[]().
std::runtime_error An exception that theoretically can not be detected by reading the code.
std::overflow_error This is thrown if a mathematical overflow occurs.
std::range_error This is occured when you try to store a value which is out of range.
std::underflow_error This is thrown if a mathematical underflow occurs.

Leave a Reply