Native/C++

exception, try, catch, throw

aucd29 2013. 10. 2. 19:03
[code]
// Ex5_04.cpp Using exception handling
#include <iostream>
using namespace std;

int main()
{
    int Height = 0;
    double InchesToMeters = 0.0254;
    char ch = 'y';

    while(ch == 'y'||ch =='Y')
    {
        cout << "Enter a height in inches: ";
        cin >> Height;        // Read the height to be converted

        try                 // Defines try block in which
        {                     // exceptions may be thrown
            if(Height > 100)
                throw "Height exceeds maximum"; // Exception thrown
            if(Height < 9)
                throw "Height below minimum";     // Exception thrown
            cout << static_cast<double>(Height*InchesToMeters)
                << " meters" << endl;
            cout << "Do you want to continue (y or n)?";
            cin >> ch;
        }

        // start of catch block to catch exceptions of type char*
        catch(const char* aMessage) //
        {                     //
            cout << aMessage << endl;
        }
    }

    return 0;
}
[/code]