无法创建两个继承自std :: logic_error的自定义异常类

时间:2017-02-06 07:37:25

标签: c++ exception-handling inheriting-constructors

我对cs类的赋值是创建两个继承自std :: logic_error的自定义异常类:OverflowingSwimmingPoolException和UnderflowingSwimmingPoolException。尝试非法操作时,创建并抛出自定义异常,而不是仅打印错误消息。在驱动程序代码中包含try ... catch块以捕获任何异常。

这是我头文件的一部分:

    #ifndef SWIMMINGPOOL_H
    #define SWIMMINGPOOL_H
    #include <stdexcept>
    #include <iostream>
    using namespace std;



 namespace cs52
  {
          class OverflowingSwimmingPoolException: public logic_error
  {

    OverflowingSwimmingPoolException (){};

};

class UnderflowingSwimmingPoolException: public logic_error
{

    UnderflowingSwimmingPoolException(){};
};

这里是编译器在构造函数所在的行上说: &#39; cs52 :: UnderflowingSwimmingPoolException&#39;的构造函数必须显式初始化基类&#39; std :: logic_error&#39;它没有默认的构造函数。

这就是我在实现文件中的内容:

    #include "SwimmingPool.h"
    #include <stdexcept>
    #include <iostream>
     using namespace std;



     namespace cs52
     {       
      SwimmingPool operator +(const SwimmingPool& pool1, const SwimmingPool&         pool2) throw (OverflowingSwimmingPoolException, UnderflowingSwimmingPoolException)
{
    SwimmingPool temp;
    temp.mySize = pool1.mySize+pool2.mySize;
    temp.myContents = pool1.myContents+pool2.myContents;
    if (temp.myContents>temp.mySize)
   throw OverflowingSwimmingPoolException();
    return temp;
}
SwimmingPool operator -(const SwimmingPool& pool1, const SwimmingPool& pool2) throw (OverflowingSwimmingPoolException, UnderflowingSwimmingPoolException)
{
    SwimmingPool temp;
    temp.mySize= pool1.mySize-pool2.mySize;
    temp.myContents= pool1.myContents-pool2.myContents;
    if (temp.myContents>temp.mySize)
        throw OverflowingSwimmingPoolException();
    if (temp.myContents<0 || temp.mySize<0)
        throw UnderflowingSwimmingPoolException();
    return temp;
}
}

编译器在我抛出异常类的行中显示错误。它说:调用类cs53的私有构造函数:OverflowingSwimmimgPoolException。

我的驱动程序文件的部分应该是这样的:

   using namespace cs52;
   try {
    SwimmingPool badPool = smallOne - bigOne;
    cout << "This should never happen..." << endl;
    } 
    catch( UnderflowingSwimmingPoolException uspe ) {
    cout << "working..." << endl;
    } 
    catch( OverflowingSwimmingPoolException uspe ) {
    cout << "This should never happen..." << endl;
    }

我刚刚开始编写代码,所以我真的不明白像库中已经创建的std :: logic_error这样的类是如何工作的。

3 个答案:

答案 0 :(得分:1)

在派生异常的构造函数中,必须调用基类which takes a string containing some error text的构造函数,如下所示:

@foreach ($students as $student) 
<div>
<img src="{{ asset('/assets/students/' . $student->id .'/thumbnail.png') }}" alt="" width="50px" height="50px">
</div>
@endforeach

当您在捕获的异常上调用OverflowingSwimmingPoolException () : std::logic_error("It's all gone horribly wrong!") {}; 时(假设您没有使用不同的行为覆盖它;但不要这样做),将返回此内容:

what()

异常类型的接口通常会为此目的获取字符串,但当然对于您自己的异常类型,您不必这样做。

请注意,如果您使用基类try { // ... } catch (std::exception const& e) { std::cerr << e.what() << '\n'; // Prints "It's all gone horribly wrong!" } 例外,请确保通过引用(或引用到catch)来捕获,以避免object slicing

答案 1 :(得分:1)

错误非常明确,与逻辑无关或何时,何地以及如何抛出异常。它只是关于异常类本身和构造函数。

注意错误说你必须初始化父类?您可以使用构造函数初始化列表来执行此操作,例如,

const

OverflowingSwimmingPoolException () : std::logic_error("Some error message") {} 初始化中的错误消息是what函数将报告的内容。

答案 2 :(得分:0)

编译器消息&#34;必须显式初始化基类&#st; :: logic_error&#39;它没有默认的构造函数&#34;是因为std::logic_error在初始化期间需要参数。具体来说,它需要一个字符串。例如:

class my_exception: public std::logic_error {
    my_exception() { };
}

......不起作用,但......

class my_exception: public std::logic_error {
    my_exception(const char* what): std::logic_error(what) { };
}

...会因为没有构造函数std::logic_error::logic_error(),但只有一个std::logic_error::logic_error(const char*)

如果您想指定例外&#34;原因&#34; (由what)静态地为特定的异常类提供,您可以在构造期间指定它,例如......

class my_exception: public std::logic_error {
    my_exception(): std::logic_error("Oh No!") { };
}

所有标准异常都从std::exception继承,期望出现异常的原因。后来,这个原因可以类似于......

try {
    throw my_exception();
} catch (const std::exception& e) { // << because you inherited this class
    std::cerr << "error: " << e.what() << std::endl;
}

...会打印&#34;错误:哦不!&#34;使用前面的例子。

具体来说,请参阅std::logic_errorstd::exception了解详情。

相关问题