C++ Exception Library - invalid_argument



Description

It is an invalid argument exception and some components of the standard library also throw exceptions of this type to signal invalid arguments.

Declaration

Following is the declaration for std::invalid_argument.

class invalid_argument;

C++11

class invalid_argument;

Parameters

none

Return Value

none

Exceptions

No-throw guarantee − no members throw exceptions.

Members

  • constructor − what_arg has the same content as the value returned by member what.

  • what − It is used to get string identifying exception.

Example

In below example explains about std::invalid_argument.

#include <iostream>       
#include <stdexcept>      
#include <bitset>         
#include <string>         

int main (void) {
   try {    
      std::bitset<5> mybitset (std::string("9848011223"));
   }
   catch (const std::invalid_argument& ia) {
      std::cerr << "Invalid argument: " << ia.what() << '\n';
   }
   return 0;
}

Let us compile and run the above program, this will produce the following result −

Invalid argument: bitset::_M_copy_from_ptr
exception.htm
Advertisements