You can use a conditional expression as a throw
expression. as shown in the following example:
// This example illustrates a conditional expresion // used as a throw expression. #include <iostream.h> void main() { int doit = 1; int dont = 0; float f = 8.9; int i = 7; int j = 6; try { throw(doit ? i : f); } catch (int x) { cout << "Caught int " << x << endl; } catch (float x) { cout << "Caught float " << x << endl; } catch (double x) { cout << "Caught double " << x << endl; } catch (...) { cout << "Caught something " << endl; } }
This example produces the following output because j is of
type int:
Caught float 7
At first glance, it looks as if the block that catches integer
values should do the catch, but i is converted to a float value
in the try block because it is in a conditional expression with
the float value f. If the try block in the example is replaced
with the following try block:
try { throw doit ? i : j; }
The following output is produced:
Caught int 7
Related Information