The overflow_error exception C++ program example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Catching the overflow_error exception in C++ programming

To show: The overflow_error exception C++ program example



// overflow_error exception, reserved storage is not enough

#include <bitset>

#include <iostream>

using namespace std;


int main(void)




// template based

bitset<100> bitset;

bitset[99] = 1;

bitset[0] = 1;

// to_ulong(), converts a bitset object to the integer that would generate the sequence of bits

unsigned long Test = bitset.to_ulong();


catch(exception &err)


cerr<<"Caught "<<err.what()<<endl;

cerr<<"Type "<<typeid(err).name()<<endl;




Output example:


Caught bitset<N> overflow

Type class std::overflow_error

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index