我有一個底座和一個派生的異常,公衆內部類店:異常不會被逮住
//base class - ProductException
class ProductException: exception
{
protected:
const int prodNum;
public:
//default+input constructor
ProductException(const int& inputNum=0);
//destructor
~ProductException();
virtual const char* what() const throw();
};
//derived class - AddProdException
class AddProdException: ProductException
{
public:
//default+input constructor
AddProdException(const int& inputNum=0);
//destructor
~AddProdException();
//override base exception's method
virtual const char* what() const throw();
};
此功能,將引發派生的異常:
void addProduct(const int& num,const string& name) throw(AddProdException);
void Store::addProduct(const int& num,const string& name)
{
//irrelevant code...
throw(AddProdException(num));
}
和調用一個函數函數並試圖捕獲一個異常:
try
{
switch(op)
{
case 1:
{
cin>>num>>name;
st.addProduct(num,name);
break;
}
}
}
...
catch(Store::ProductException& e)
{
const char* errStr=e.what();
cout<<errStr;
delete[] errStr;
}
派生類應該被捕獲,但是我k eep得到錯誤「未處理的異常」。任何想法爲什麼?謝謝!
這麼愚蠢的錯誤。謝謝! – nodwj