Throwing Several Operands with C++ Exceptions
Exception Handling in C++ - Part 2
Forward: In this part of the series we see how several exception operands can be thrown in C++.
By: Chrysanthus Date Published: 24 Aug 2012
Introduction
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.
Different Ways of throwing Several Operands
A try block can have several throw statements, and each statement throwing a particular (one) operand. A throw statement can also throw an array. We look at these two ways of throwing several operands.
Several throw Statements in a try-block
You can have several throw statements in a try block. What matters here is that the operand of each throw statement should be of a type different from the other throw statements. There should be the same number of corresponding catch blocks.
At this point I have to make some clarification between what we learned in the previous part of the series and what we are learning now. If in the try block, you have several throw statements of the same type, then you need one catch block for all those throws. In the catch block if-statements can be used to select the different operand values. What we are learning now is this: If in the try block, you have several throw statements each of a different type, then you need corresponding catch blocks for the different types. Each of these catch blocks would not need if-statements to select any operand value, because the criterion for a catch block to catch a throw is only that the parameter type of the catch block should be the same as the type of the thrown operand.
#include <iostream>
using namespace std;
int main()
{
try
{
throw 5; //an int
throw 4.6; //a double (for float)
throw 'B'; //a char
throw "some text"; //a string
}
catch (int i)
{
cout << "I caught an int." << "\n";
}
catch (double d)
{
cout << "I caught a double." << "\n";
}
catch (char c)
{
cout << "I caught a char." << "\n";
}
catch (const char *str)
{
cout << "I caught a string through a pointer to constant char." << "\n";
}
return 0;
}
There are other things to note: If you want a float type, use the double type in the throw and catch constructs. If you want to throw a string, use a pointer to a constant char (const char *) for the parameter of the catch.
Each of the throw statements in the try block above can be in an if-block; but remember, after a throw execution, the statements below the throw until the end of the try block are not executed.
Note: If an exception (thrown operand) is not caught, (maybe because there is no appropriate catch block), your complete C++ program may terminate (stop functioning).
Note: Another name for the catch block is the handler. Also note that an exception is considered caught (from a throw) when any catch block receives it. It does not matter what the catch block does with the thrown operand. Under that condition your program is safe from terminating, unless your catch block has wrong coding.
What a catch block uses to catch an exception is the type of the operand that is thrown (and not even the value of the operand). If you want to consider the value of the operand, then you have to use an if-statement in the corresponding type catch block.
Consider the following code:
#include <iostream>
using namespace std;
int main()
{
int arrInt[] = {25, 20, 256};
try
{
throw arrInt;
}
catch (int *const ePtr)
{
cout << ePtr[0] << "\n";
cout << ePtr[1] << "\n";
cout << ePtr[2] << "\n";
}
return 0;
}
In the main function an array is initialized. You then have a try and a catch block. In the try block, the array is thrown. The parameter of the catch block is a constant pointer (where the pointer-address and not the pointed value is constant). In the catch block, the pointer is used as an array; the pointer is pointing to the array thrown, not a new array. You have one try block and one catch block. The try block has one throw statement. The statement throws an array, which may have several elements. These elements (operands) are of the same type; so you need one catch block for them.
We have seen two ways of throwing several operands. In the first way, only one throw statement is executed. If the throw statements are in if statements, then the one whose if-condition is satisfied is the one that is thrown. If the throw statements are not in if-statements, as in the sample above, hardly would you have that, then the first one is thrown. The second way is to throw an array. When an array is thrown, the operand of the throw statement is the array name without the [] brackets. The parameter of the catch block is a constant pointer. You use the pointer identifier with the [] brackets like an array in the catch block. This pointer is pointing to the array thrown, not a new object (array). In both ways, there is only one try block.
Let us end here for this part of the series. We continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT