CSCE 120 — Exam 2 Reference Sheet
Exceptions | Streams | Pointers | Memory | Arrays | Classes | OOP
1 EXCEPTIONS & DATA VALIDATION
Exception Hierarchy Try / Catch
std::exception try {
std::runtime_error int x = stoi("hello");
overflow_error, range_error }
std::logic_error catch(std::invalid_argument const& e){
std::invalid_argument cout << e.what(); // "stoi"
std::out_of_range }
catch(std::out_of_range const& e){
cout << e.what();
}
Throwing catch(...){ // LAST: catches anything
#include <stdexcept> cout << "unknown error";
throw std::runtime_error("msg"); }
throw std::invalid_argument("bad");
throw std::out_of_range("too big");
Exception Propagation
• If f() throws & doesnt catch → travels up call stack
Integer Limit Checks • If main() doesnt catch → program crashes
#include <limits> // OR #include <climits> • Execution resumes after the matching catch block
std::numeric_limits<int>::max() // = INT_MAX • Catch in same function that threw = usually wrong
std::numeric_limits<int>::min() // = INT_MIN
// Check BEFORE arithmetic (not after!): stoi / stod
if (a > INT_MAX - b) // overflow check
throw std::out_of_range("overflow"); #include <string>
return a + b; int x = stoi("120"); // x = 120
int y = stoi("hello"); // throws invalid_argument
int z = stoi("9999999999");// throws out_of_range
WRONG: if(x+y > INT_MAX) — x+y already overflowed! double d = stod("3.14"); // d = 3.14
RIGHT: if(x > INT_MAX - y) — check before the op.
ALWAYS catch by const& — not by value!
Catch by value causes "slicing": child info lost.
e.what() returns the string msg from the constructor.
, 2 STREAMS (File, String, Formatting)
Headers & Types Stream Error Flags
Header Provides Flag Meaning
#include <fstream> ifstream, ofstream, fstream eof End of file reached
#include <sstream> istringstream, ostringstream fail Bad read (recoverable)
#include <iomanip> setprecision, setw, fixed... bad Stream corrupted
File Output (ofstream) stream.fail() // true if fail or bad set
stream.eof() // true if eof set
std::ofstream ofs("out.txt"); // overwrites if(stream){} // same as !stream.fail()
if(ofs.is_open()) ofs << "hi" << endl;
// Append mode: // Fix a failed read:
std::fstream fs("f.txt", std::ios::out|std::ios::app); stream.clear(); // 1. reset flags
stream.ignore(1000,"\n"); // 2. skip bad data
// OR: string garbage; stream >> garbage;
File Input (ifstream)
std::ifstream ifs("data.txt");
int x; ifs >> x; // read one int Output Buffer
// Read whole file word-by-word: • Output is buffered — may not appear if program crashes
std::string w; • endl flushes buffer; \n does NOT
while(ifs >> w) { /* process w */ }
• cout.flush() also works
String Streams iomanip Formatting
#include <sstream> double n = 123.4567;
std::istringstream iss("10 20 30"); cout << n; // 123.457 (6 sig figs)
int a,b,c; iss >> a >> b >> c; cout << setprecision(4) << n; // 123.5
cout << fixed << n; // 123.4567 (4 dec)
std::ostringstream oss; cout << scientific << n; // 1.2346e+02
oss << "val=" << 42; cout << setw(10) << n; // " 123.457" (next only!)
std::string s = oss.str(); // "val=42" cout << left << setw(10) << n; // "123.457 "
cout << boolalpha << true; // "true"
Getline + istringstream (COMMON PATTERN)
Seeking
std::string line;
std::getline(std::cin, line); // read full line fs.seekp(n); // move write pos to byte n
std::istringstream iss(line); fs.seekg(n); // move read pos to byte n
int sum=0, x=0; fs.tellp(); fs.tellg(); // get positions
while(iss >> x) sum += x; // NOTE: writing overwrites existing chars!
open() / close()
std::ofstream ofs("file1.txt");
ofs << "hello";
ofs.close();
ofs.open("file2.txt");
ofs << "goodbye";
Exceptions | Streams | Pointers | Memory | Arrays | Classes | OOP
1 EXCEPTIONS & DATA VALIDATION
Exception Hierarchy Try / Catch
std::exception try {
std::runtime_error int x = stoi("hello");
overflow_error, range_error }
std::logic_error catch(std::invalid_argument const& e){
std::invalid_argument cout << e.what(); // "stoi"
std::out_of_range }
catch(std::out_of_range const& e){
cout << e.what();
}
Throwing catch(...){ // LAST: catches anything
#include <stdexcept> cout << "unknown error";
throw std::runtime_error("msg"); }
throw std::invalid_argument("bad");
throw std::out_of_range("too big");
Exception Propagation
• If f() throws & doesnt catch → travels up call stack
Integer Limit Checks • If main() doesnt catch → program crashes
#include <limits> // OR #include <climits> • Execution resumes after the matching catch block
std::numeric_limits<int>::max() // = INT_MAX • Catch in same function that threw = usually wrong
std::numeric_limits<int>::min() // = INT_MIN
// Check BEFORE arithmetic (not after!): stoi / stod
if (a > INT_MAX - b) // overflow check
throw std::out_of_range("overflow"); #include <string>
return a + b; int x = stoi("120"); // x = 120
int y = stoi("hello"); // throws invalid_argument
int z = stoi("9999999999");// throws out_of_range
WRONG: if(x+y > INT_MAX) — x+y already overflowed! double d = stod("3.14"); // d = 3.14
RIGHT: if(x > INT_MAX - y) — check before the op.
ALWAYS catch by const& — not by value!
Catch by value causes "slicing": child info lost.
e.what() returns the string msg from the constructor.
, 2 STREAMS (File, String, Formatting)
Headers & Types Stream Error Flags
Header Provides Flag Meaning
#include <fstream> ifstream, ofstream, fstream eof End of file reached
#include <sstream> istringstream, ostringstream fail Bad read (recoverable)
#include <iomanip> setprecision, setw, fixed... bad Stream corrupted
File Output (ofstream) stream.fail() // true if fail or bad set
stream.eof() // true if eof set
std::ofstream ofs("out.txt"); // overwrites if(stream){} // same as !stream.fail()
if(ofs.is_open()) ofs << "hi" << endl;
// Append mode: // Fix a failed read:
std::fstream fs("f.txt", std::ios::out|std::ios::app); stream.clear(); // 1. reset flags
stream.ignore(1000,"\n"); // 2. skip bad data
// OR: string garbage; stream >> garbage;
File Input (ifstream)
std::ifstream ifs("data.txt");
int x; ifs >> x; // read one int Output Buffer
// Read whole file word-by-word: • Output is buffered — may not appear if program crashes
std::string w; • endl flushes buffer; \n does NOT
while(ifs >> w) { /* process w */ }
• cout.flush() also works
String Streams iomanip Formatting
#include <sstream> double n = 123.4567;
std::istringstream iss("10 20 30"); cout << n; // 123.457 (6 sig figs)
int a,b,c; iss >> a >> b >> c; cout << setprecision(4) << n; // 123.5
cout << fixed << n; // 123.4567 (4 dec)
std::ostringstream oss; cout << scientific << n; // 1.2346e+02
oss << "val=" << 42; cout << setw(10) << n; // " 123.457" (next only!)
std::string s = oss.str(); // "val=42" cout << left << setw(10) << n; // "123.457 "
cout << boolalpha << true; // "true"
Getline + istringstream (COMMON PATTERN)
Seeking
std::string line;
std::getline(std::cin, line); // read full line fs.seekp(n); // move write pos to byte n
std::istringstream iss(line); fs.seekg(n); // move read pos to byte n
int sum=0, x=0; fs.tellp(); fs.tellg(); // get positions
while(iss >> x) sum += x; // NOTE: writing overwrites existing chars!
open() / close()
std::ofstream ofs("file1.txt");
ofs << "hello";
ofs.close();
ofs.open("file2.txt");
ofs << "goodbye";