mardi 25 décembre 2018

Getting an error while deriving class from std::exception

I am deriving a class from std::exception but i got an error Here is my code

#include "stdafx.h"
#include <iostream>

using namespace std;

class exp1 : public std::exception {
public:
    exp1() noexcept = default;
    ~exp1() = default;
    virtual const char* what() const noexcept
    {
        return "This is an exception";
    }
};


int main()
{
    try{
        int i; 
        cin >> i;
        if(i == 0)throw exp1() ;
        else {cout << i << endl;}
       }
    catch(exp1 & ex){
        cout << ex.what() << endl; 
       }
return 0;
}

My code is working fine but when i include noexcept to the constructor

exp1() noexcept = default;

then i get the errors

'exp1::exp1(void) noexcept': attempting to reference a deleted function 

and

the declared exception specification is incompatible with the generated one 

Aucun commentaire:

Enregistrer un commentaire