Namespaces
Variants

std:: format_error

From cppreference.net
헤더 파일에 정의됨 <format>
class format_error : public runtime_error
(C++20부터)

서식 지정 라이브러리에서 오류를 보고하기 위해 던져지는 예외 객체의 유형을 정의합니다.

std::format_error 의 모든 멤버 함수는 constexpr 입니다: 상수 표현식 평가 중에 std::format_error 객체를 생성하고 사용하는 것이 가능합니다.

그러나 std::format_error 객체는 일반적으로 constexpr 일 수 없습니다. 동적으로 할당된 저장 공간은 모두 동일한 상수 표현식 평가 내에서 해제되어야 하기 때문입니다.

(C++26부터)
cpp/error/exception cpp/error/runtime error std-format error-inheritance.svg

상속 다이어그램

목차

멤버 함수

(생성자)
주어진 메시지로 새로운 format_error 객체를 생성합니다
(public 멤버 함수)
operator=
format_error 객체를 대체합니다
(public 멤버 함수)

std::format_error:: format_error

format_error ( const std:: string & what_arg ) ;
(1) (constexpr since C++26)
format_error ( const char * what_arg ) ;
(2) (constexpr since C++26)
format_error ( const format_error & other ) noexcept ;
(3) (constexpr since C++26)
1) 설명 문자열로 what_arg 을 사용하여 예외 객체를 생성합니다. 생성 후, std:: strcmp ( what ( ) , what_arg. c_str ( ) ) == 0 입니다.
2) 설명 문자열로 what_arg 을 사용하여 예외 객체를 생성합니다. 생성 후, std:: strcmp ( what ( ) , what_arg ) == 0 입니다.
3) 복사 생성자입니다. * this other 모두 std::format_error 동적 타입을 가진다면 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. 복사 생성자에서는 예외가 발생하지 않습니다.

매개변수

what_arg - 설명 문자열
other - 복사할 다른 예외 객체

예외

1,2) std::bad_alloc 을 발생시킬 수 있습니다.

참고

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

파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 생성자를 가져야 합니다. 원본 객체와 복사된 객체에 대해 what() 으로 얻은 설명 문자열이 동일하다면 암시적으로 정의될 수 있습니다.

std::format_error:: operator=

format_error & operator = ( const format_error & other ) noexcept ;
(constexpr since C++26)

other 의 내용으로 내용을 할당합니다. * this other 모두 동적 타입 std::format_error 를 가지는 경우, 할당 후 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. 복사 할당 연산자에서는 예외가 발생하지 않습니다.

매개변수

other - 할당할 다른 예외 객체

반환 값

* this

참고 사항

파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 할당 연산자를 가져야 합니다. what() 으로 얻은 설명 문자열이 원본 객체와 복사된 객체에 대해 동일한 한 암시적으로 정의될 수 있습니다.

std:: runtime_error 에서 상속됨


std::exception에서 상속됨

멤버 함수

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

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_constexpr_exceptions 202502L (C++26) constexpr 예외 타입

예제

#include <format>
#include <print>
#include <string_view>
#include <utility>
int main()
{
    try
    {
        auto x13{37};
        auto args{std::make_format_args(x13)};
        std::ignore = std::vformat("{:()}", args); // throws
    }
    catch(const std::format_error& ex)
    {
        std::println("{}", ex.what());
    }
}

가능한 출력:

format error: failed to parse format-spec

참고 항목