← C++ Programming learning pathC++ Programming · Module 8 of 10
Exceptions and files
Handle errors and read/write files.
📖 Key ideas
- try { } catch (const std::exception& e) { } handles errors.
- throw std::runtime_error("msg") raises one.
- File streams: ifstream (read), ofstream (write).
- getline(file, line) reads lines.
- 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