例外でエラーを知らせる / 例外の型ごとにcatchブロックを設ける

#include <iostream>
#include <string>
using namespace std;

int main(){
    for (int i = 0; i < 5; i++) {
        try {
            switch(i) {
                case 0 : throw i               ; break; // int型の例外を投げる。
                case 1 : throw string("1.0000"); break; // string型の例外を投げる。
                case 2 : throw 2.1             ; break; // double型の例外を投げる。
                case 3 : throw i               ; break; // int型の例外を投げる。
                case 4 : throw string("4.0000"); break; // string型の例外を投げる。
                default: break;
            }
        }
        catch (int    except) { cout << except << " int型の例外発生"    << endl; }
        catch (string except) { cout << except << " string型の例外発生" << endl; }
        catch (double except) { cout << except << " double型の例外発生" << endl; }
    }

    return 0;
}

f:id:ti-nspire:20190519072600p:plain