Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ios_base::failure

      From cppreference.com
      <cpp‎ |io‎ |ios base
       
       
       
       
      Defined in header<ios>
      class failure;

      The classstd::ios_base::failure defines an exception object that is thrown on failure by the functions in the Input/Output library.

      std::ios_base::failure may be defined either as a member class ofstd::ios_base or as a synonym (typedef) for another class with equivalent functionality.

      (since C++17)
      (until C++11)
      (since C++11)

      Contents

      [edit]Member functions

      (constructor)
      constructs a newfailure object with the given message
      (public member function)
      operator=
      replaces thefailure object
      (public member function)
      what
      returns the explanatory string
      (public member function)

      std::ios_base::failure::failure

      (1)
      explicit failure(conststd::string& message);
      (until C++11)
      explicit failure(conststd::string& message,
                       conststd::error_code& ec=std::io_errc::stream);
      (since C++11)
      explicit failure(constchar* message,
                       conststd::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) Constructs the exception object usingmessage as explanation string which can later be retrieved usingwhat().ec is used to identify the specific reason for the failure.(since C++11)
      3) Copy constructor. Initialize the contents with those ofother.If*this andother both have dynamic typestd::ios_base::failure thenstd::strcmp(what(), other.what())==0.(since C++11)

      Parameters

      message - explanatory string
      ec - error code to identify the specific reason for the failure
      other - anotherfailure to copy

      Notes

      Because copyingstd::ios_base::failure is not permitted to throw exceptions, this message is typically stored internally as a separately-allocated reference-counted string. This is also why there is no constructor takingstd::string&&: it would have to copy the content anyway.

      std::ios_base::failure::operator=

      failure& operator=(const failure& other);
      (until C++11)
      failure& operator=(const failure& other)noexcept;
      (since C++11)

      Assigns the contents with those ofother.If*this andother both have dynamic typestd::ios_base::failure thenstd::strcmp(what(), other.what())==0 after assignment.(since C++11)

      Parameters

      other - another exception object to assign with

      Return value

      *this

      std::ios_base::failure::what

      virtualconstchar* what()constthrow();
      (until C++11)
      virtualconstchar* what()constnoexcept;
      (since C++11)

      Returns the explanatory string.

      Return value

      Pointer to an implementation-defined null-terminated string with explanatory information. The string is suitable for conversion and display as astd::wstring. The pointer is guaranteed to be valid at least until the exception object from which it is obtained is destroyed, or until a non-const member function (e.g. copy assignment operator) on the exception object is called.

      Notes

      Implementations are allowed but not required to overridewhat().

      Inherited fromstd::system_error

      Member functions

      returns error code
      (public member function ofstd::system_error)[edit]
      [virtual]
      returns an explanatory string
      (virtual public member function ofstd::system_error)[edit]

      Inherited fromstd::runtime_error


      Inherited fromstd::exception

      Member functions

      [virtual]
      destroys the exception object
      (virtual public member function ofstd::exception)[edit]
      [virtual]
      returns an explanatory string
      (virtual public member function ofstd::exception)[edit]

      [edit]Notes

      Before the resolution ofLWG issue 331,std::ios_base::failure declared a destructor withoutthrow(), wherestd::exception::~exception() was declared withthrow()[1]. This means thestd::ios_base::failure::~failure() had a weaker exception specification. The resolution is to remove that declaration so that the non-throwing exception specification is kept.

      LWG issue 363 targets the same defect and its resolution is to addthrow() to the declaration ofstd::ios_base::failure::~failure(). That resolution was not applied due to the conflict between the two resolutions.

      1. The non-throwing exception specification is now appliedglobally across the standard library, so the destructors of standard library classes are not declared withthrow() ornoexcept.

      [edit]Example

      Run this code
      #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';}}

      Possible output:

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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 48C++98the constructor overload (1) initialized the base classstd::exception
      withmsg, but the base class does not have a matching constructor
      corresponding
      description removed
      LWG 331C++98std::ios_base::failure declared a destructor withoutthrow()removed the destructor declaration

      [edit]See also

      (C++11)
      the IO stream error codes
      (enum)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/ios_base/failure&oldid=177686"

      [8]ページ先頭

      ©2009-2025 Movatter.jp