Namespaces
Variants

std::ios_base:: failure

From cppreference.net
헤더 파일에 정의됨 <ios>
class failure ;

클래스 std::ios_base::failure 는 입출력 라이브러리의 함수들이 실패할 때 던져지는 예외 객체를 정의합니다.

std::ios_base::failure std::ios_base 의 멤버 클래스로 정의되거나, 동등한 기능을 가진 다른 클래스의 동의어(typedef)로 정의될 수 있습니다.

(C++17부터)
cpp/error/exception std-ios base-failure-2003-inheritance.svg

상속 다이어그램

(C++11 이전)
cpp/error/exception cpp/error/runtime error cpp/error/system error std-ios base-failure-inheritance.svg

상속 다이어그램

(C++11 이후)

목차

멤버 함수

(constructor)
주어진 메시지로 새로운 failure 객체를 생성합니다
(public member function)
operator=
failure 객체를 대체합니다
(public member function)
what
설명 문자열을 반환합니다
(public member function)

std::ios_base::failure:: failure

(1)
explicit failure ( const std:: string & message ) ;
(until C++11)
explicit failure ( const std:: string & message,
const std:: error_code & ec = std:: io_errc :: stream ) ;
(since C++11)
explicit failure ( const char * message,
const std:: error_code & ec = std:: io_errc :: stream ) ;
(2) (since C++11)
(3)
failure ( const failure & other ) ;
(until C++11)
failure ( const failure & other ) noexcept ;
(since C++11)
1,2) 예외 객체를 생성하며, message 를 설명 문자열로 사용합니다. 이 문자열은 나중에 what() 을 사용하여 검색할 수 있습니다. ec 는 실패의 구체적인 원인을 식별하는 데 사용됩니다. (C++11부터)
3) 복사 생성자입니다. 내용을 other 의 내용으로 초기화합니다. 만약 * this other 모두 동적 타입이 std::ios_base::failure 라면, std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. (C++11부터)

매개변수

message - 설명 문자열
ec - 실패의 구체적인 원인을 식별하는 에러 코드
other - 복사할 다른 failure 객체

참고 사항

std::ios_base::failure 의 복사가 예외를 던지는 것을 허용하지 않기 때문에, 이 메시지는 일반적으로 별도로 할당된 참조 카운트 문자열로 내부에 저장됩니다. 이것이 또한 std:: string && 를 취하는 생성자가 없는 이유이기도 합니다: 어차피 내용을 복사해야 하기 때문입니다.

std::ios_base::failure:: operator=

failure & operator = ( const failure & other ) ;
(C++11 이전)
failure & operator = ( const failure & other ) noexcept ;
(C++11 이후)

내용을 other 의 내용으로 할당합니다. 만약 * this other 모두 동적 타입 std::ios_base::failure 를 가진다면, 할당 후 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. (C++11 이후)

매개변수

other - 할당할 다른 예외 객체

반환값

* this

std::ios_base::failure:: what

virtual const char * what ( ) const throw ( ) ;
(C++11 이전)
virtual const char * what ( ) const noexcept ;
(C++11 이후)

설명 문자열을 반환합니다.

반환 값

설명 정보를 담은 구현 정의 널 종료 문자열에 대한 포인터입니다. 이 문자열은 std::wstring 으로 변환 및 표시하기에 적합합니다. 이 포인터는 최소한 해당 포인터를 얻은 예외 객체가 소멸되거나, 예외 객체의 비상수 멤버 함수(예: 복사 할당 연산자)가 호출되기 전까지 유효함이 보장됩니다.

참고

구현체는 what() 을 재정의할 수 있지만 필수는 아닙니다.

std:: system_error 로부터 상속됨

멤버 함수

에러 코드를 반환함
( std::system_error 의 public 멤버 함수)
[virtual]
설명 문자열을 반환함
( std::system_error 의 virtual public 멤버 함수)

std:: runtime_error 에서 상속됨


std:: exception 에서 상속됨

멤버 함수

[virtual]
예외 객체를 파괴함
( std::exception 의 virtual public 멤버 함수)
[virtual]
설명 문자열을 반환함
( std::exception 의 virtual public 멤버 함수)

참고 사항

LWG 이슈 331 이 해결되기 전에는, std::ios_base::failure throw ( ) 없이 소멸자를 선언했으며, 여기서 std::exception::~exception() throw ( ) 로 선언되었습니다 [1] . 이는 std::ios_base::failure::~failure() 가 더 약한 예외 명세를 가졌음을 의미합니다. 해결책은 해당 선언을 제거하여 예외를 던지지 않는 예외 명세를 유지하는 것입니다.

LWG 이슈 363 은 동일한 결함을 대상으로 하며, 그 해결책은 throw ( ) std::ios_base::failure::~failure() 의 선언에 추가하는 것입니다. 두 해결책 간의 충돌로 인해 해당 해결책은 적용되지 않았습니다.

  1. 예외를 발생시키지 않는 예외 명세는 이제 표준 라이브러리 전체에 걸쳐 전역적으로 적용 되므로, 표준 라이브러리 클래스들의 소멸자는 throw ( ) 또는 noexcept 로 선언되지 않습니다.

예제

#include <fstream>
#include <iostream>
int main()
{
    std::ifstream f("doesn't exist");
    try
    {
        f.exceptions(f.failbit);
    }
    catch (const std::ios_base::failure& e)
    {
        std::cout << "Caught an ios_base::failure.\n"
                  << "Explanatory string: " << e.what() << '\n'
                  << "Error code: " << e.code() << '\n';
    }
}

가능한 출력:

Caught an ios_base::failure.
Explanatory string: ios_base::clear: unspecified iostream_category error
Error code: iostream:1

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 48 C++98 생성자 오버로드 (1)이 기본 클래스 std::exception
msg 로 초기화했지만, 기본 클래스에는 일치하는 생성자가 없음
해당
설명 제거됨
LWG 331 C++98 std::ios_base::failure throw ( ) 없이 소멸자를 선언함 소멸자 선언 제거됨

참고 항목

(C++11)
IO 스트림 오류 코드
(열거형)