← C++ Programming learning path
C++ Programming · Module 8 of 10

Exceptions and files

Handle errors and read/write files.

📖 Key ideas

  1. try { } catch (const std::exception& e) { } handles errors.
  2. throw std::runtime_error("msg") raises one.
  3. File streams: ifstream (read), ofstream (write).
  4. getline(file, line) reads lines.
  5. Streams close automatically at scope end (RAII).
💡 Real-life example: Exceptions are emergency exits; file streams are taps you open to pour data in or out.
try/catchError handling
throwRaises an exception
ifstreamInput file stream
ofstreamOutput file stream