【问题标题】:Error handling inside of classes类内部的错误处理
【发布时间】:2015-09-12 21:10:36
【问题描述】:

假设我在类构造函数中有这段代码:

if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
    std::cerr << "Couldn't init SDL2 video" << std::endl;
    std::cerr << SDL_GetError() << std::endl;
}

如何使用 try、throw、catch 代替 if、cerr 来处理错误?我应该使用检查错误的成员函数(返回 bool),然后使用 try、throw、catch 进行错误处理吗?

构造函数:

GLWindow::GLWindow(int width, int height, std::string name) {
    // Init SDL Video
    if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
    std::cerr << "Couldn't init SDL2 video" << std::endl;
    std::cerr << SDL_GetError() << std::endl;
    }

    // Forward compatibility
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);

    // Main window
    glWindow = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);

    // Check if Main window is created
    if(glWindow == NULL) {
        std::cerr << "Couldn't create main window" << std::endl;
        std::cerr << SDL_GetError() << std::endl;
    }

    // GL Context for Main Window
    glContext = SDL_GL_CreateContext(glWindow);

    // Check if GL Context is created
    if(glContext == NULL) {
        std::cerr << "Couldn't create GL context for main window" << std::endl;
        std::cerr << SDL_GetError() << std::endl;
    }
}

【问题讨论】:

  • 不要将异常用于一般的流控制,它们用于异常情况。除非您在构造函数中,否则我看不出它们的使用在这里如何被认为是合理的。只需返回一个成功/失败值。
  • 这是在构造函数中,这就是我遇到问题的原因
  • 那么您应该编辑您的帖子并在其中包含该信息。我建议添加一个示例构造函数以及如何使用它来使其清晰。
  • 如果出现错误,我正在尝试退出程序。有人告诉我,我可以通过 main() 中的异常处理来做到这一点(这是创建对象的地方),但我是异常处理的新手,在类中似乎有点困难。

标签: c++ class error-handling


【解决方案1】:

我相信你可以这样做:

try {
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
        throw std::runtime_error("Couldn't init 2D video");
    }
}

catch(const std::runtime_error &e) {
    std::cout << e.what() << std::endl;
    std::cout << SDL_GetError() << std::endl;
}

【讨论】:

    【解决方案2】:

    根据评论:

    “如果出现错误,我会尝试退出程序。有人告诉我,我可以通过 main() 中的异常处理来做到这一点(这是 对象是制作的)但我是异常处理的新手,这似乎是 在课堂上更难一点。”

    我会说你可以打电话给std::terminate() 这将退出你的程序。

    例如:

    #include<exception>//std::terminate() 
    //the rest of your code 
    
    //...
    if(glContext == NULL) {
        std::cerr << "Couldn't create GL context for main window" << std::endl;
        std::cerr << SDL_GetError() << std::endl;
        std::terminate();//exit program on error
        //you don't have to get rid of the std::cerr calls if you don't want to 
        //because in theory you could rout them to the terminal or some error log
        //that you could use for debugging or error logging purposes
    }
    //...
    

    虽然我认为这样做很可能不是处理错误的最佳方法。我相信 Andreas DM 的回答将是朝着正确方向迈出的良好开端。

    查看这篇文章,了解有关异常的一些信息: https://isocpp.org/wiki/faq/exceptions#ctor-exceptions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 2013-06-16
      • 2023-01-30
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多