// --------------
// Exceptions.c++
// --------------

#include <cassert>   // assert
#include <cstring>   // strcmp
#include <iostream>  // cout, endl
#include <stdexcept> // domain_error, range_error

void f (bool b) {
    if (b)
        throw std::domain_error("abc");
    throw std::range_error("def ghi");
    assert(false);}

void g () {
    try {
        f(true);
        assert(false);
        }
    catch (const std::range_error& e) {
        assert(false);}
    assert(false);}

void h () {
    try {
        f(true);
        assert(false);
        }
    catch (const std::domain_error& e) {
        assert(std::strcmp(e.what(), "abc") == 0);
        throw;                                     // throw e;
        assert(false);}
    catch (const std::range_error& e) {
        assert(false);}
    assert(false);}

int main () {
    using namespace std;
    cout << "Exceptions.c++" << endl;

    try {
        f(true);
        assert(false);
        }
    catch (const domain_error& e) {
        assert(strcmp(e.what(), "abc") == 0);}
    catch (const range_error& e) {
        assert(false);}

    try {
        f(false);
        assert(false);
        }
    catch (const domain_error& e) {
        assert(false);}
    catch (const range_error& e) {
        assert(strcmp(e.what(), "def ghi") == 0);}

    try {
        g();
        assert(false);
        }
    catch (const domain_error& e) {
        assert(strcmp(e.what(), "abc") == 0);}
    catch (const range_error& e) {
        assert(false);}

    try {
        h();
        assert(false);
        }
    catch (const domain_error& e) {
        assert(strcmp(e.what(), "abc") == 0);}
    catch (const range_error& e) {
        assert(false);}

    cout << "Done." << endl;
    return 0;}


syntax highlighted by Code2HTML, v. 0.9.1