User-Defined Exceptions

Description:

Two numbers are entered by user for division operation. If the dividend is zero, then division by zero will cause exception which is thrown into catch block. If the answer is less than 0, then exception "Division is less than 1" is thrown. All other exceptions are handled by the last catch block throwing "Unknown" exception. Test Case 1 Input (stdin) 8 5 Expected Output 1.6 Test Case 2 Input (stdin) 1 0 Expected Output Exception: Division by zero

Program :

#include <iostream>

using namespace std;


int main()

{

    int a,b;

   // cout << "Enter 2 numbers: ";

    cin >> a >> b;

    try

    {

        if (b != 0)

        {

            float div = (float)a/b;

            if (div < 0)

                throw 'e';

            cout << div;

        }

        else

            throw b;

           

    }

    catch (int e)

    {

        cout << "Exception: Division by zero";

    }

    catch (char st)

    {

        cout << "Exception: Division is less than 1";

    }

    catch(...)

    {

        cout << "Exception: Unknown";

    }

   

    return 0;

}