https://rentry.org/PPP2_p152a

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

#include <iostream>
#include <stdexcept>

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

void error(const char* s) { throw runtime_error(s); }

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

  return 0;  // 0 indicates success

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

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

build & run:

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

PrevUpNext

Edit
Pub: 23 Feb 2023 23:14 UTC
Edit: 29 Apr 2023 09:37 UTC
Views: 395