出色的文件处理

时间:2018-09-29 06:30:27

标签: c++ exception-handling

我是C ++的新手,我正尝试使用c ++实施出色的文件处理。 在以下代码中,我从基本类异常继承了 Class Divide_By_Zero_Exception 类。运行代码后,我遇到了错误

  

错误:“ {”令牌之前的预期类名   {

如果要公开继承它,为什么要指定类名,如果需要指定如何做,为什么要指定它。

//program to throw an exception if denominator is zero
#include <iostream>
#include <exception>
class Divide_By_Zero_Exception : public exception
{
public:
    const char * what() const throw() {
        return "Divide By Zero Exception";
    }
};
using namespace std;
int main()
{
    Divide_By_Zero_Exception d;
    int n1, n2;
    cin >> n1 >> n2;
    try
    {
        if (n2 == 0)
            throw d;
        else
            cout << n1 / n2;
    }
    catch (exception& e) {
        cout << e.what();
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

它是std::exception而不是exception,因为当您使用该名称时,您的using指令尚未生效。

也可以代替

const char * what() const throw()

您可能想写

const char * what() const noexcept
相关问题