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

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

Program 1:

#include <iostream>
#include <exception>

using namespace std;

union 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:

main.cpp:6:30: error: derived union ‘UserException’ invalid
 union UserException : public exception {
                              ^~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:16:29: error: invalid use of incomplete type ‘union UserException’
         throw UserException();
                             ^
main.cpp:6:7: note: forward declaration of ‘union UserException’
 union UserException : public exception {
       ^~~~~~~~~~~~~
main.cpp:18:26: error: invalid use of incomplete type ‘union UserException’
     catch (UserException E) {
                          ^
main.cpp:6:7: note: forward declaration of ‘union UserException’
 union UserException : public exception {
       ^~~~~~~~~~~~~
main.cpp:19:17: error: ‘E’ was not declared in this scope
         cout << E.message();
                 ^

Explanation:

It will generate a compilation error because we cannot inherit any class into the "union". Here we inherited exception class into "UserException" union.

Program 2:

#include <iostream>
#include <exception>

using namespace std;

void expFun(char* ptr)
{
    if (ptr == NULL)
        throw "NULL Exception";
}
int main()
{
    expFun(NULL);
    catch (const char* E)
    {
        cout << E;
    }

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:14:5: error: expected primary-expression before ‘catch’
     catch (const char* E)
     ^~~~~

Explanation:

It will generate the compile-time error, because we did not create any "try" block but we used catch block in the above program, so we need to define a "try" block to resolve the problem.

Program 3:

#include <iostream>
#include <exception>

using namespace std;

void expFun(char* ptr)
{
    try {
        if (ptr == NULL)
            throw "NULL Exception";
    }
}

int main()
{
    expFun(NULL);
    catch (const char* E)
    {
        cout << E;
    }

    return 0;
}

Output:

main.cpp: In function ‘void expFun(char*)’:
main.cpp:12:1: error: expected ‘catch’ before ‘}’ token
 }
 ^
main.cpp:12:1: error: expected ‘(’ before ‘}’ token
main.cpp:12:1: error: expected type-specifier before ‘}’ token
main.cpp:12:1: error: expected ‘)’ before ‘}’ token
main.cpp:12:1: error: expected ‘{’ before ‘}’ token
main.cpp: In function ‘int main()’:
main.cpp:17:5: error: expected primary-expression before ‘catch’
     catch (const char* E)
     ^~~~~

Explanation:

It will generate the compile-time error because in the main() function, we did not create any "try" block but we used catch block in the above program, so we need to define a "try" block in the same function with a catch block.

Note: The “try” and “catch” must be present in the same function.

Program 4:

#include <iostream>
#include <exception>

using namespace std;

void expFun(char* ptr)
{
    if (ptr == NULL)
        throw "NULL Exception";
}

int main()
{
    try {
        expFun(NULL);
    }
    catch (const char* E) {
        cout << E;
    }
    return 0;
}

Output:

NULL Exception

Explanation:

Here, we defined a function expFun() with the character pointer as an argument, and we check if the value of pointer ptr is NULL then it will throw a constant string that will be caught by the corresponding catch block.

In the main() function. Here we called expFun() function in the try block and pass NULL as an argument, that’s why function expFun() thrown an exception which is caught by catch block and print the message on the console screen.

Program 5:

#include <iostream>
using namespace std;

int main()
{
    try {
        try {
            throw 101;
        }
        catch (int num) {
            cout << "Here we re-throw the exception" << endl;
            throw;
        }
    }
    catch (int num) {
        cout << "Exception code: " << num << endl;
    }
    return 0;
}

Output:

Here we re-throw the exception
Exception code: 101

Explanation:

Here, we created a nested try and catch block, here we thrown an integer number 101, then caught and print the message after that re-throw the exception using "throw" keyword, and finally caught the exception and print received exception code by the outer catch block.






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.