Throw
2002-06-19 11:57:34
Category: cpp:general
Description: Basic example of throwing exceptions.
Author: detour
Viewed: 2219
Rating: (3 votes)


/* throw.cpp by detour@metalshell.com
 *
 * Basic example of throwing an exception.
 *
 * http://www.metalshell.com/
 *
 */
 
#include <iostream>
#include <string>
 
using namespace std;
 
string day_of_week(int now) {
    string dow[] = { "Monday", "Tuesday", "Wednesday", "Thursday", 
                     "Friday", "Saturday", "Sunday" };
 
    if(now < 1 || now > 7)
        throw;
    else
        return dow[now - 1];
}
 
int main() {
 
    try {
        cout << day_of_week(1);
        cout << day_of_week(3);
        cout << day_of_week(15);
    } catch(...) {
        cerr << "Out of range!";
    }
 
    return 0;
}