CS253: Software Development with C++

Spring 2018

Exception Handling

See this page as a slide show

Exception Handling

CS253 Exception Handling

Normal Operation

#include "Loud.h"

Loud a('a');

void foo() {
    Loud b('b');
    Loud c('c');
}

int main() {
    Loud d('d');
    foo();
    Loud e('e');
    return 0;
}
a: Loud::Loud(char)
d: Loud::Loud(char)
b: Loud::Loud(char)
c: Loud::Loud(char)
c: Loud::~Loud()
b: Loud::~Loud()
e: Loud::Loud(char)
e: Loud::~Loud()
d: Loud::~Loud()
a: Loud::~Loud()

throw without catch

#include "Loud.h"

Loud a('a');

void foo() {
    Loud b('b');
    throw 42;
    Loud c('c');
}

int main() {
    Loud d('d');
    foo();
    Loud e('e');
    return 0;
}
a: Loud::Loud(char)
d: Loud::Loud(char)
b: Loud::Loud(char)
terminate called after throwing an instance of 'int'
SIGABRT: Aborted

You can throw anything—doesn’t have to be a special type or object.

try #1

#include "Loud.h"

Loud a('a');

void foo() {
    Loud b('b');
    throw "oops!";
    Loud c('c');
}

int main() {
    Loud d('d');
    try {
        foo();
    }
    catch (const char *error) {
        cout << "Caught: " << error << "\n";
    }

    Loud e('e');
    return 0;
}
a: Loud::Loud(char)
d: Loud::Loud(char)
b: Loud::Loud(char)
b: Loud::~Loud()
Caught: oops!
e: Loud::Loud(char)
e: Loud::~Loud()
d: Loud::~Loud()
a: Loud::~Loud()

try #2

#include "Loud.h"

Loud a('a');

void foo() {
    Loud b('b');
    throw "oops!";
    Loud c('c');
}

void bar() {
    Loud d('d');
    foo();
}

int main() {
    Loud e('e');
    try {
        bar();
    }
    catch (const char *error) {
        cout << "Caught: “" << error << "”\n";
    }

    Loud f('f');
    return 0;
}
a: Loud::Loud(char)
e: Loud::Loud(char)
d: Loud::Loud(char)
b: Loud::Loud(char)
b: Loud::~Loud()
d: Loud::~Loud()
Caught: “oops!”
f: Loud::Loud(char)
f: Loud::~Loud()
e: Loud::~Loud()
a: Loud::~Loud()

catch #1

try {
    throw "oops!";
}

catch (int i) {
    cout << "int " << i << "\n";
}

catch (const char *error) {
    cout << "C string: " << error << "\n";
}

catch (...) {
    cout << "something\n";
}
C string: oops!

catch #2

try {
    throw 42;
}

catch (short s) {
    cout << "Got a short: " << s << "\n";
}

catch (long l) {
    cout << "Got a long: " << l << "\n";
}
terminate called after throwing an instance of 'int'
SIGABRT: Aborted

The type must match. No conversions!

catch #3

// logic_error is-a exception
// runtime_error is-a exception
// overflow_error is-a runtime_error

try {
    overflow_error oops("OVERFLOW!");
    throw oops;
}
catch (const logic_error &e) {
    cout << "logic_error: " << e.what() << '\n';
}
catch (const runtime_error &e) {
    cout << "runtime_error: " << e.what() << '\n';
}
catch (const exception &e) {
    cout << "exception: " << e.what() << '\n';
}
runtime_error: OVERFLOW!

catch #4

// logic_error is-a exception
// runtime_error is-a exception
// overflow_error is-a runtime_error

try {
    overflow_error oops("OVERFLOW!");
    throw oops;
}
catch (const logic_error &e) {
    cout << "logic_error: " << e.what() << '\n';
}
catch (const exception &e) {
    cout << "exception: " << e.what() << '\n';
}
exception: OVERFLOW!

No conversions, but is-a is good enough.

re-throw

void foo() {
    throw string("Division by zero");
}

void bar() {
    try {
        foo();
    }
    catch (string msg) {
        if (msg == "Out of memory")  // I’ve got this!
            /* get more memory */;
        else
            throw;      // Throw up hands in despair.
    }
}

int main() {
    try {
        bar();
    }
    catch (string problem) {
        cout << "Problem in bar: " << problem << '\n';
    }

    return 0;
}
Problem in bar: Division by zero

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-03-06T15:54

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building