As we know constructor has no return type, so we cannot return value from constructor. However we can throw an exception from the constructor. Here is example.

#include<iostream>
class A {

public:
A ( int i)
{

       if ( i == 5)
           throw "error";

}

};

int main()

{

       try{   A a(5); }

       catch ( char* s){

       std::cout<<str<<endl;

       }

}



Related Contents to follow