I've created a custom exception class testException.
throw creates a testException object which upon creation receives the desired name of the exception.
When a testException is caught (by reference) it should return the name of the exception by using the member function Get.Exception.
For some reason the function isn't called and instead I get the error:
terminate called after throwing an instance of testException
I saw a similar example here, which supposedly should work.
- Why do I get the above error?
Code:
Exception.h
#include <iostream>
#include <string>
#ifndef TESTEXCEPTION_H
#define TESTEXCEPTION_H
using std::cout;
using std::cin;
using std::endl;
using std::string;
class testException {
public:
testException(string);
string GetException();
~testException();
protected:
private:
string myexception;
};
#endif // TESTEXCEPTION_H
Exception.cpp
#include "testException.h"
testException::testException(string temp) : myexception(temp) {
// ctor
}
string testException::GetException() {
return myexception;
}
testException::~testException() {
// dtor
}
main.h
#include <iostream>
#include "testException.h"
using std::cout;
using std::cin;
using std::endl;
int main() {
throw testException ("Test");
try {
// Shouldn't be printed if exception is caught:
cout << "Hello World" << endl;
} catch (testException& first) {
std::cerr << first.GetException();
}
return 0;
}