本文介绍了标准C ++库中有什么异常类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准C ++库中包含的异常类是什么,它们应该用于什么?我知道有一些新的C ++ 11例外,但我不知道他们是什么,或者他们在哪里。

解决方案

pre> std :: exception< exception>界面(如果你应该抓住这个话)有争议)
std :: bad_alloc< new>无法分配存储
std :: bad_array_new_length< new>无效的数组长度
std :: bad_cast< typeinfo>执行无效的动态转换
std :: bad_exception< exception>表示抛出一个不正确的异常
std :: bad_function_call< functional>抛出nullstd :: function
std :: bad_typeid< typeinfo>在空指针上使用typeinfo
std :: bad_weak_ptr< memory>从bad_ptr
std :: logic_error< stdexcept>构造一个shared_ptr程序执行前检测到的错误
std :: domain_error< stdexcept>参数超出有效范围
std :: future_error< future>违反了std :: promise / std :: future条件
std :: invalid_argument< stdexcept>无效参数
std :: length_error< stdexcept>长度超过其最大允许大小
std :: out_of_range< stdexcept>参数值不在其预期范围内
std :: runtime_error< stdexcept>程序执行时可检测到的错误
std :: overflow_error< stdexcept>算术溢出错误。
std :: underflow_error< stdexcept>算术下溢误差。
std :: range_error< stdexcept>内部计算中的范围错误
std :: regex_error< regex>来自正则表达式库的错误。
std :: system_error< system_error>来自操作系统或其他C API
std :: ios_base :: failure< ios>输入或输出错误

资料来源:

实际上,大多数异常都是自定义异常派生自 logic_error runtime_error 。不是这些被忽略,但是许多例外是域特定的。



请记住,异常应该反映出什么问题,而不是谁扔了(否MyProgramException)


What are the exception classes that are included in the standard C++ library, and what should they be used for? I know there are a few new C++11 exceptions, but I'm not sure what they are or where they are.

解决方案
std::exception <exception> interface (debatable if you should catch this)
    std::bad_alloc <new> failure to allocate storage
        std::bad_array_new_length <new> invalid array length
    std::bad_cast <typeinfo> execution of an invalid dynamic-cast
    std::bad_exception <exception> signifies an incorrect exception was thrown
    std::bad_function_call <functional> thrown by "null" std::function
    std::bad_typeid <typeinfo> using typeinfo on a null pointer
    std::bad_weak_ptr <memory> constructing a shared_ptr from a bad weak_ptr
    std::logic_error <stdexcept> errors detectable before the program executes
        std::domain_error <stdexcept> parameter outside the valid range
        std::future_error <future> violated a std::promise/std::future condition
        std::invalid_argument <stdexcept> invalid argument
        std::length_error <stdexcept> length exceeds its maximum allowable size
        std::out_of_range <stdexcept> argument value not in its expected range
    std::runtime_error <stdexcept> errors detectable when the program executes
        std::overflow_error <stdexcept> arithmetic overflow error.
        std::underflow_error <stdexcept> arithmetic underflow error.
        std::range_error <stdexcept> range errors in internal computations
        std::regex_error <regex> errors from the regular expression library.
        std::system_error <system_error> from operating system or other C API
            std::ios_base::failure <ios> Input or output error

Source: http://en.cppreference.com/w/cpp/error/exception
In practice, most exceptions are custom exceptions derived from logic_error and runtime_error. Not that these are neglected, but that many exceptions are domain specific.

Keep in mind that an exception should reflect what went wrong and not who threw it. (No "MyProgramException"s)

这篇关于标准C ++库中有什么异常类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:23