Broad Network


Specifying Exceptions to Functions in C++

Exception Handling in C++ - Part 4

Forward: In this part of the series, I show you how to precise the type of exceptions that a function can throw.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is part 4 of my series, Exception in C++. You can have a try-catch construct in a function. You can then go on to decide the type of operands that the try block in the function can throw as exception. In this part of the series, I show you how to precise the type of exceptions that a function can throw.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Try-Catch Construct in a Function
All along we have been having try-catch constructs in the main function. You can still have it in some other function. The following code illustrates this:

#include <iostream>
using namespace std;

    void aFn (int aa, double bb)
        {
            try
             {
                    if ((aa<0)||(aa>10))
                        throw aa;
                    if ((bb<50)||(bb>70))
                        throw bb;

                    //do something with aa and bb
                    cout << "aa is " << aa << "\n";
                    cout << "bb is " << bb << "\n";
             }
            catch (int eInt)
                {
                    cout << "Integer is not in range!";
                }
            catch (double eDbl)
                {
                    cout << "Double is not in range!";
                }
        }

int main()
    {
        aFn (-5, 66.6);

        return 0;
    }

The function has two arguments: the first one is an int and the second one is a double (float). The int should lie between 0 and 10 and the double should lie between 50 and 70. The try block checks this and throws the corresponding parameter identifier accordingly. There is a catch block for the int and there is a catch block for the double. The emphasis here is that the try block and the two catch blocks are in a function.

Do not confuse between the situation here and what we saw in part 1 where a try block called a function that had the throw statement. Here, the try block and its throw statement(s) and the catch block(s) are in one function.

Specifying Exceptions to the above Function
The operands for the throw statements in the above function are of type int and double. You can precise that any operand for a throw statement in the try bock of the function must be either int or double. The following code illustrates this:

#include <iostream>
using namespace std;

    void aFn (int aa, double bb) throw (int, double)
        {
            try
             {
                    if ((aa<0)||(aa>10))
                        throw aa;
                    if ((bb<50)||(bb>70))
                        throw bb;

                    //do something with aa and bb
                    cout << "aa is " << aa << "\n";
                    cout << "bb is " << bb << "\n";
             }
            catch (int eInt)
                {
                    cout << "Integer is not in range!";
                }
            catch (double eDbl)
                {
                    cout << "Double is not in range!";
                }
        }

int main()
    {
        aFn (-5, 66.6);

        return 0;
    }

Note the expression, “throw (int, double)” at the end of the declaration (top) of the function.

The syntax for specifying exception to a function is

    returnType fnName (parameters) throw (types);

The specification is done at the declaration of the function. You just attach “throw (types)” to the declaration of the function. You can have one or more types in the parentheses of the throw expression at the declaration.

Remember: you can have the declaration (ending with semicolon) of a function in one part of your code and then have the declaration and definition in another part of the code.

Notes
- A function with no exception specification is a function that does not have the “throw (types)” expression at its declaration. Such a function allows all exceptions (allows throws in the try-block with operands of any type).
- A function with an empty exception specification, that is throw() at the declaration, does not allow any exception; that is its try block cannot throw anything. Such a function should not have try and catch blocks.
- If a function has exception specification and it throws an exception that is not specified, the whole program may terminate.

Let us end here for this part of the series. We continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message