Namespaces
Variants

std::filesystem:: filesystem_error

From cppreference.net
헤더 파일에 정의됨 <filesystem>
class filesystem_error ;
(C++17부터)

클래스 std::filesystem::filesystem_error 는 파일시스템 라이브러리의 함수들 중 예외를 발생시키는 오버로드들이 실패 시 던지는 예외 객체를 정의합니다.

cpp/error/exception cpp/error/runtime error cpp/error/system error std-filesystem-filesystem error-inheritance.svg

상속 다이어그램

목차

멤버 함수

예외 객체를 생성합니다
(public member function)
예외 객체를 대체합니다
(public member function)
오류를 발생시킨 작업과 관련된 경로를 반환합니다
(public member function)
설명 문자열을 반환합니다
(public member function)

std::system_error로부터 std:: system_error 상속됨

멤버 함수

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

std:: runtime_error 에서 상속됨


std::exception에서 상속됨

멤버 함수

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

참고 사항

filesystem_error 의 복사 함수들이 noexcept임을 보장하기 위해, 일반적인 구현에서는 what() 의 반환값과 path1() path2() 에 의해 참조되는 두 개의 std::filesystem::path 객체를 보유하는 객체를 별도로 할당된 참조 카운트 저장소에 저장합니다.

현재 MS STL 구현 은 표준을 따르지 않습니다: 위에서 언급된 객체들이 filesystem 객체 내에 직접 저장되어 있어 복사 함수들이 noexcept가 아닙니다.

예제

#include <filesystem>
#include <iostream>
#include <system_error>
int main()
{
    const std::filesystem::path from{"/none1/a"}, to{"/none2/b"};
    try
    {
        std::filesystem::copy_file(from, to); // throws: files do not exist
    }
    catch (std::filesystem::filesystem_error const& ex)
    {
        std::cout << "what():  " << ex.what() << '\n'
                  << "path1(): " << ex.path1() << '\n'
                  << "path2(): " << ex.path2() << '\n'
                  << "code().value():    " << ex.code().value() << '\n'
                  << "code().message():  " << ex.code().message() << '\n'
                  << "code().category(): " << ex.code().category().name() << '\n';
    }
    // All functions have non-throwing equivalents
    std::error_code ec;
    std::filesystem::copy_file(from, to, ec); // does not throw
    std::cout << "\nNon-throwing form sets error_code: " << ec.message() << '\n';
}

가능한 출력:

what():  filesystem error: cannot copy file: No such file or directory [/none1/a] [/none2/b]
path1(): "/none1/a"
path2(): "/none2/b"
code().value():    2
code().message():  No such file or directory
code().category(): generic
Non-throwing form sets error_code: No such file or directory