std::basic_ios::exceptions(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::basic_ios::exceptions πŸ’–

okay bestie, welcome to the std::basic_ios::exceptions(3) page from the C++ Standard Libary ✨ β€” yes, that's the manual name, and we're keeping it cute and exact. This whole page is about std::basic_ios::exceptions, and it's lowkey iconic how much one little exception mask can do. πŸ’…


✨ NAME ✨

std::basic_ios::exceptions β€” yup, the name is literally std::basic_ios::exceptions, and the original NAME line repeats it too: std::basic_ios::exceptions - std::basic_ios::exceptions. No plot twist, just main-character repetition. 🫢

🌸 Synopsis 🌸

std::ios_base::iostate exceptions() const;        (1)
void exceptions( std::ios_base::iostate except ); (2)

Okay so this is the dynamic duo: it gets and sets the exception mask of the stream. The exception mask is the thing that determines which error states trigger exceptions of type failure. Not me explaining stream lore like it's gossip, but here we are. πŸ’€

  1. Returns the exception mask. That's the current one, no cap.
  2. Sets the exception mask to except. And bestie, listen: if the stream has an error state covered by the exception mask when called, an exception is immediately triggered. Like, instantly. 🚨

πŸŽ€ Parameters πŸŽ€

  • except β€” exception mask. That's the whole parameter, bestie. Slay. πŸ’–

🌈 Return value 🌈

  1. The current exception mask. That's what overload (1) hands back. ✨
  2. (none) β€” overload (2) returns nothing, literally no return value. 🫢

πŸ¦‹ Notes πŸ¦‹

🚧 This section is incomplete.

Reason: discuss LWG2349 and link from ios_base::clear, and from (un)formatted(i/o)utputfunction requirement pages (or perhaps the behavior should be fully elaborated on the requirement pages and linked from here). That's the note, and we're keeping all the exact bits because we do not erase the lore. πŸ’œ

πŸ’œ See also πŸ’œ

  • stackoverflow.com/a/35089910 β€” the linked Stack Overflow answer. Go click, bestie. πŸ¦‹

πŸ“ Example πŸ“

// Run this code

#include <fstream>
#include <iostream>

int main()
{
    int ivalue;
    try
    {
        std::ifstream in("in.txt");
        in.exceptions(std::ifstream::failbit); // may throw
        in >> ivalue; // may throw
    }
    catch (const std::ios_base::failure& fail)
    {
        // handle exception here
        std::cout << fail.what() << '\n';
    }
}

πŸ‘‘ Possible output πŸ‘‘

Possible output:

basic_ios::clear: iostream error

πŸ’… Category πŸ’…

  • Todo with reason

signed, sealed, and served by http://cppreference.com on 2024.06.10 πŸ’‹ β€” this was std::basic_ios::exceptions(3), source and version/date stamp included, and she ate. πŸŽ€βœ¨

πŸ“¦ where this page comes from