The invalid_argument exception usage in 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 invalid_argument exception for the try-catch C++ constructs

To show: The invalid_argument C++ program example, catching the invalid argument

 

// invalid_argument

#include <bitset>

#include <iostream>

using namespace std;

 

int main(void)

{

try

{

// binary wrongly represented by char X

// template based

bitset<32> bitset(string("0101001X01010110000"));

}

catch (exception &err)

{

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

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

};

}

 

Output example:

 

Caught invalid bitset<N> char

Type class std::invalid_argument

Press any key to continue . . .

 

 

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