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

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

Program 1:

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

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

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

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

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

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

Output:

Exception generated

Explanation:

Here, we defined a function funDiv() with two arguments X and Y. We checked if the value of Y is 0 then it will throw an object of bad_exception class using the "throw" keyword that will be caught by the "catch" block.

Here, we also used "exception" class in the catch block and thrown object of bad_exception class. It will work fine because the "exception" class is the superclass of all exception related classes. 

In the main() function, we declared local variables 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.

Program 2:

#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 (bad_exception e) {
        cout << "Exception generated1";
    }
    catch (...) {
        cout << "Exception generated2";
    }
    return 0;
}

Output:

Exception generated2

Explanation:

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

We also used two catch blocks, the first catch block will catch the object of bad_exception class, and the second catch block "catch(…)" is a global catch block, then it will receive all types of exceptions.

Program 3:

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

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

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

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

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

Output:

Floating point exception

Explanation:

It will crash at runtime because here we did not handle the "divide by zero" exception.

The global catch block "catch(…)" will catch only those exceptions that are thrown by the "throw" keyword. So we need to handle the exception and throw using the "throw" keyword.

Program 4:

#include <iostream>
#include <exception>

using namespace std;

class UserException : public exception {
public:
    const char* message() const throw()
    {
        return "My User Exception";
    }
};

int main()
{
    try {
        throw UserException();
    }
    catch (UserException E) {
        cout << E.message();
    }
    return 0;
}

Output:

My User Exception

Explanation:

Here, we created a class UserException which is inherited by the "exception" class, and we defined the member function message() inside the class UserException. The message() function returns the message "My User Exception".

In the main() function, we have thrown an object of UserException class, which is caught by the catch block. Here we printed the message returned by message () function.

Program 5:

#include <iostream>
#include <exception>

using namespace std;

struct UserException : public exception {
    const char* message() const throw()
    {
        return "My User Exception";
    }
};

int main()
{
    try {
        throw UserException();
    }
    catch (UserException E) {
        cout << E.message();
    }
    return 0;
}

Output:

My User Exception

Explanation:

Here, we created a structure UserException which is inherited by the "exception" class, and we defined the member function message() inside the structure UserException. The message() function returns the message "My User Exception".

In the main() function, we have thrown an object of UserException structure, which is caught by the catch block. Here we printed the message returned by message () function.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.