https://rentry.org/PPP2_p165a

// Code derived from Stroustrup's PPP2 book
// § 5.10 Pre- and post-conditions
//  -and beginning on p 165

#include <iostream>
#include <stdexcept>

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

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

/*
// the arguments are positive and a < b < c
int my_complicated_function(int a, int b, int c)
{
  if (! (0 < a && a < b && b < c))  // ! means “not” and && means “and”
    error("bad arguments for mcf");

  // . . .

  return 1;  // stub
}
*/

int my_complicated_function(int a, int b, int c)
{
  // . . .

  return 1;  // stub
}

int main()
try {
  int x = my_complicated_function(1, 2, "horsefeathers");

  cout << x << '\n';

} 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_p165a.cpp && ./a.out

PrevUpNext

Edit
Pub: 25 Feb 2023 12:56 UTC
Edit: 29 Apr 2023 10:10 UTC
Views: 402