https://rentry.org/PPP2_p152b

// Code derived from Stroustrup's PPP2 book
// § 5.6.3 Bad input
//  -and beginning on p 152

#include <iostream>
#include <stdexcept>
#include <string>

using std::cerr;
using std::exception;
using std::runtime_error;
using std::string;

void error(string s1, string s2) { throw runtime_error(s1 + s2); }

int main()
try {
  // . . . our program . . .

  return 0;  // 0 indicates success

} catch (exception& e) {
  cerr << "error: " << e.what() << '\n';
  // keep_window_open();
  return 1;

} catch (...) {
  cerr << "Oops: unknown exception!\n";
  // keep_window_open();
  return 2;
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p152b.cpp && ./a.out

PrevUpNext

Edit
Pub: 24 Feb 2023 07:52 UTC
Edit: 29 Apr 2023 09:39 UTC
Views: 370