【问题标题】:Exception Handling in C++ by throwing a string通过抛出字符串在 C++ 中进行异常处理
【发布时间】:2013-04-03 17:52:18
【问题描述】:

我的程序如下:(在 linux 上)

// Ex. 2 of Exception Handling
// Here divn() raises the exception but main() will be the exception handler

#include<iostream>
using namespace std;

int divn(int n, int d)
{
    if(d == 0)
        throw "Division by ZERO not possible ";

    return n/d;
}

main()
{
    int numer, denom;
    cout << "Enter the numerator and denominator : ";
    cin >> numer >> denom;
    try
    {
        cout << numer << " / " << denom << " = " << divn(numer,denom) << endl;
    }
    catch(char *msg)
    {
        cout << msg;
    }
    cout << "\nEnd of main() \n ";
}

/*当我们将分母设为0时,它应该抛出异常并提供给定的错误消息。当我输入分母为0时得到的输出如下:

administrator@ubuntu:~/FYMCA/CPP_class$ g++ prog110.cpp 管理员@ubuntu:~/FYMCA/CPP_class$ ./a.out 输入分子 和分母: 12 0 在抛出一个实例后终止调用 'char const*' 中止(核心转储)

我该如何解决这个问题?

【问题讨论】:

    标签: c++


    【解决方案1】:

    字符串文字的类型为char const[],衰减为char const*。您应该相应地调整您的 catch 处理程序:

    catch (char const* msg)
    //          ^^^^^
    {
        cout << msg;
    }
    

    这是live example

    最后,这里有一个更好的方法来重写你的程序,使用来自 C++ 标准库的异常类:

    #include <iostream>
    #include <stdexcept>
    //       ^^^^^^^^^^^ For std::logic_error
    
    int divn(int n, int d)
    {
        if(d == 0)
        {
            throw std::logic_error("Division by ZERO not possible ");
            //    ^^^^^^^^^^^^^^^^
            //    Throw by value
        }
    
        return n/d;
    }
    
    int main() // <== You should specify the return type of main()!
    {
        // Rather use these than "using namespace std;"
        using std::cout;
        using std::cin;
        using std::endl;
    
        int numer, denom;
        cout << "Enter the numerator and denominator : ";
        cin >> numer >> denom;
    
        try
        {
            cout << numer << " / " << denom << " = " << divn(numer,denom) << endl;
        }
        catch(std::logic_error const& err)
        //    ^^^^^^^^^^^^^^^^^^^^^^^
        //    Catch by reference
        {
            cout << err.what();
            //      ^^^^^^^^^^
        }
    
        cout << "\nEnd of main() \n ";
    }
    

    当然还有对应的live example

    【讨论】:

      【解决方案2】:

      异常消息表明,你抓错了类型。

      【讨论】:

        【解决方案3】:

        您不应该以这种方式抛出异常。一个好的做法是在 stdexcept lib 中使用标准异常,例如 std::runtime_error 或 std::logic 错误,或者基于 std::exception 创建自己的异常类

        【讨论】:

          【解决方案4】:

          一个简单的解决方法是在顶部添加如下语句:

          char * err = "Division by ZERO not possible";
          

          然后,将您的 throw 更改为 throw err;

          这与编译器如何为字符串文字分配存储空间有关。

          【讨论】:

          • 在 C++11 中转换为char* 是非法的
          • 另外,您应该将 main() 更改为 int main() 并包含一个 return ( 0 );最后。
          • 我确实更改了我的main(),而return 0 不需要main()
          猜你喜欢
          • 2019-07-19
          • 2015-01-26
          • 1970-01-01
          • 2011-12-12
          • 2013-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多