×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Exceptional Handling | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Exceptional Handling (set 1).
Submitted by Nidhi, on July 19, 2020

Program 1:

#include <iostream>
using namespace std;

int main()
{
    try {
        int num1 = 10;
        int num2 = 0;
        int res = 0;

        res = num1 / num2;
    }
    catch (exception e) {
        cout << "Exception: Divide By Zero" << endl;
    }
    return 0;
}

Output:

Floating point exception

Explanation:

Here, we created two code blocks "try" and "catch". We declared 3 local variables in num1, num2, and res.

Here, variable num1 is initialized with 10 and num2 initialized with 0 then the below expression will generate a runtime exception:

res = num1/num2;

Because the above expression will generate the situation of "Divide By Zero". But here we did not use "throw" to throw the exception, that's why the program will crash at runtime.

Program 2:

#include <iostream>

#define DEVIDE_BY_ZERO 101
using namespace std;

int main()
{
    try {
        int num1 = 10;
        int num2 = 0;
        int res = 0;

        if (num2 == 0)
            throw DEVIDE_BY_ZERO;
        res = num1 / num2;
    }
    catch (int exp_code) {
        cout << "Exception code: " << exp_code << endl;
    }
    return 0;
}

Output:

Exception code: 101

Explanation:

Here, we created two code blocks "try" and "catch". We declared 3 local variables in num1, num2, and res.

Here, variable num1 is initialized with 10 and num2 initialized with 0 then the below expression will generate a runtime exception:

res = num1/num2;

But here we handled the exception; if num2 is 0 then defined error code will be thrown and caught by "catch" block.

In the catch block, we printed the received exception code using cout.

Program 3:

#include <iostream>
using namespace std;

int main()
{
    try {
        int num1 = 10;
        int num2 = 0;
        int res = 0;

        if (num2 == 0)
            throw "Divide By Zero";
        res = num1 / num2;
    }
    catch (char* exp) {
        cout << "Exception: " << exp << endl;
    }
    return 0;
}

Output:

terminate called after throwing an instance of 'char const*'
Aborted (core dumped)

Explanation:

The above crashed at runtime because here we have thrown a constant string ("Divide By Zero") using "throw" keyword, and using char *exp in the "catch" block.

To resolve the problem we need to use const char *exp instead of char *exp.

Program 4:

#include <iostream>
using namespace std;

void funDiv(int X, int Y)
{
    int res = 0;

    if (Y == 0)
        throw "Divide By Zero";
    res = X / Y;
    cout << res << endl;
}

int main()
{
    try {
        int num1 = 10;
        int num2 = 0;

        funDiv(num1, num2);
    }
    catch (const char* exp) {
        cout << "Exception: " << exp << endl;
    }
    return 0;
}

Output:

Exception: Divide By Zero

Explanation:

Here, we defined a function funDiv() with two arguments X and Y. Here, we checked if the value of Y is 0 then it will throw a string message using the "throw" keyword.

Now coming to the main() function, here we declared local variable num1 and num2 inside the “try” block, and call funDiv() function to perform division.

The string message is thrown by funDiv() function, caught by the "catch" block, and print the message using cout on the console screen.

Program 5:

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

void funDiv(int X, int Y)
{
    int res = 0;

    if (Y == 0) {
        exception E;
        throw E;
    }

    res = X / Y;
    cout << res << endl;
}

int main()
{
    try {
        int num1 = 10;
        int num2 = 0;

        funDiv(num1, num2);
    }
    catch (exception) {
        cout << "Exception generated";
    }
    return 0;
}

Output:

Exception generated

Explanation:

Here, we defined a function funDiv() with two arguments X and Y. Here, we checked if the value of Y is 0 then it will throw an object of exception class using the "throw" keyword.

Now coming to the main() function, here we declared local variable num1 and num2 inside the “try” block, and call funDiv() function to perform division.

The exception object was thrown by funDiv() function, caught by the "catch" block, and print the message using cout on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.