CS253

CS253: Software Development with C++

Spring 2018

Old-fashioned Error Handling

See this page as a slide show

Old-fashioned Error Handling

CS253 Old-fashioned Error Handling

The Old Days

exit

#include "Loud.h"

Loud a='a';

void foo() {
    Loud b('b');
    exit(1);
}

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

abort

#include "Loud.h"

Loud a='a';

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

int main() {
    Loud c='c';
    foo();
    Loud d='d';
    return 0;
}
a: Loud::Loud(char)
c: Loud::Loud(char)
b: Loud::Loud(char)
SIGABRT: Aborted

assert

#include "Loud.h"

Loud a('a');

void foo() {
    Loud b('b');
    assert(2>3);
}

int main() {
    Loud c('c');
    foo();
    Loud d('d');
    return 0;
}
a: Loud::Loud(char)
c: Loud::Loud(char)
b: Loud::Loud(char)
a.out: c.cc:7: void foo(): Assertion `2>3' failed.
SIGABRT: Aborted

division by zero

#include "Loud.h"

Loud a('a');

int n = 0;

void foo() {
    Loud b('b');
    n = 1/n;
}

int main() {
    Loud c('c');
    foo();
    Loud d('d');
    return 0;
}
a: Loud::Loud(char)
c: Loud::Loud(char)
b: Loud::Loud(char)
SIGFPE: Floating point exception

Modified: 2018-01-02T10:18

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building