표준 C ++ 라이브러리에있는 예외 클래스


102

표준 C ++ 라이브러리에 포함 된 예외 클래스는 무엇이며 어떤 용도로 사용해야합니까? 몇 가지 새로운 C ++ 11 예외가 있다는 것을 알고 있지만 그것이 무엇인지, 어디에 있는지 잘 모르겠습니다.

답변:


124
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

출처 : http://en.cppreference.com/w/cpp/error/exception
실제로 대부분의 예외는 logic_error및 에서 파생 된 사용자 지정 예외 runtime_error입니다. 이것이 무시된다는 것은 아니지만 많은 예외가 도메인에 따라 다릅니다.

예외는 누가 그것을 던졌는 지가 아니라 무엇이 잘못되었는지를 반영해야한다는 것을 명심하십시오 . ( "MyProgramException"없음)


bad_function_call, domain_error, and future_errormsdn에서 그들은 최악의 예이며 설명되었습니다 :(
Mr. Anubis

bad_function_call기본 생성 된 std :: function 객체가 있고 래핑 된 함수를 호출하려고 할 때 발생합니다. 래핑 된 함수가 없기 때문에 호출 할 것이 없습니다.
Pete Becker

1
bad_function_callstd::function준비되지 않은 호출 을 시도 할 때 발생합니다 (즉, 기본 구성 또는 nullptr을 통해 명시 적으로 지워짐). 및 future_error에 대한 함수의 여러 전제 조건 중 하나를 위반할 때 사용됩니다 . 그리고 (이론적으로) 함수에 대한 입력이 해당 함수의 유효한 범위를 벗어난 경우 (예 :의 음수 )입니다. promisefuturedomain_errorstd::sqrt
Dave S

future_error요청 된 작업이 유효하지 않거나 객체를 유효하지 않은 상태로 만들 때 Future에 대한 다양한 작업에 의해 발생합니다. 이것은 C ++ 11의 새로운 기능이며 주석에 튜토리얼을 넣을 수 없습니다.
Pete Becker

3
cppreference에서의 파생 클래스 목록 std::exception과 그들 C ++ (11) (특히,인지 메모 std::ios_base::failure에서 이동 std::exception하는 std::system_error). 사용법과 헤더는 하나의 링크입니다.
ecatmur

50

사이트 보기

여기에 이미지 설명 입력

Exception               Description
===================================
std::exception          An exception and parent class of all the standard C++ exceptions.
std::bad_alloc          This can be thrown by new.
std::bad_cast           This can be thrown by dynamic_cast.
std::bad_exception      This is useful device to handle unexpected exceptions in a C++ program
std::bad_typeid         This can be thrown by typeid.
std::logic_error        An exception that theoretically can be detected by reading the code.
std::domain_error       This is an exception thrown when a mathematically invalid domain is used
std::invalid_argument   This is thrown due to invalid arguments.
std::length_error       This is thrown when a too big std::string is created
std::out_of_range       This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[]().
std::runtime_error      An exception that theoretically can not be detected by reading the code.
std::overflow_error     This is thrown if a mathematical overflow occurs.
std::range_error        This is occured when you try to store a value which is out of range.
std::underflow_error    This is thrown if a mathematical underflow occurs.

이것은 좋지만 C ++ 11 예외가 누락되었으며 어떤 예외가 어떤 헤더에 있는지 표시하지 않습니다.
Mooing Duck 2012-08-13

3
@MooingDuck 귀하의 질문에 태그되었습니다 c++아니라 c++11, 그들은 같은 모든 상주<stdexcept>
TemplateRex

6
C ++는 최신 버전이 무엇이든 상관없이 C ++ 11 및 C ++ 03은 특정 버전에 대한 질문 입니다. 내 질문은 특정 버전에 관한 것이 아니라 C ++에 대한 최신 정보입니다. 어느 쪽이든 C ++ 11을 언급하도록 질문을 편집하겠습니다. 또한 ideone.com/uqM6h<stdexcept>
Mooing Duck의

1
@MooingDuck 특별히 질문하지 않으면 C ++ 03에 대한 답변은 C ++ 11에 대한 답변만큼 유효하며 그 반대의 경우도 마찬가지입니다. 필요한 모든 정보를 제공하는 것은 귀하의 책임입니다. 불쌍한 질문에서 양질의 답변을 기 대해서는 안됩니다. 기간.
Phil1970

std::logic_error, 아니라 std::logic_failure. 그 다이어그램이 잘못되었습니다!
Galaxy
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.