C++ Exception Library - Constructor


Advertisements

Description

It is constructor exception.

Declaration

Following is the declaration for std::exception::exception.

	
exception() throw();
exception (const exception& e) throw();

C++11

	
exception() noexcept;
exception (const exception& e) noexcept;

Parameters

e − It is an another exception object.

Return Value

none

Exceptions

No-throw guarantee − no members throw exceptions.

Example

In below example for std::exception::exception.

#include <iostream>       
#include <exception>      

struct ooops : std::exception {
   const char* what() const noexcept {return "Exception test!\n";}
};

int main () {
   ooops e;
   std::exception* p = &e;
   try {
      throw e;       
   } catch (std::exception& ex) {
      std::cout << ex.what();
   }
   try {
      throw *p;      
   } catch (std::exception& ex) {
      std::cout << ex.what();
   }
   return 0;
}

The sample output should be like this −

Exception test!
std::exception 
exception.htm
Advertisements