Topic: Syntax of C++ exception expressions
Author: schmidt@liege.ics.uci.edu (Douglas C. Schmidt)
Date: 12 Jul 1994 20:15:39 -0700 Raw View
Hi,
I'm trying to determine the proper syntax for throwing C++
exception expressions. The following code compiles and runs as
expected using SunC++ 4.0:
----------------------------------------
1 #include <iostream.h>
2
3 class Base
4 {
5 public:
6 struct RANGE_ERROR {
7 RANGE_ERROR (int i = 10): j (i) {}
8 int j;
9 };
10 };
11
12 class Derived : private Base
13 {
14 public:
15 Derived (void) {
16 throw Base::RANGE_ERROR ();
17 }
18
19 Base::RANGE_ERROR;
20 };
21
22 int main (void)
23 {
24 try
25 {
26 Derived d;
27 }
28 catch (Derived::RANGE_ERROR &e)
29 {
30 cout << "exception caught " << e.j << "!\n";
31 }
32 return 0;
33 }
----------------------------------------
However, when line 16 is changed to
16 throw Base::RANGE_ERROR;
The Sun C++ compiler complains:
----------------------------------------
"x.C", line 16: Error: Base::RANGE_ERROR may not be a type name.
"x.C", line 16: Error: Badly formed expression.
2 Error(s) detected.
----------------------------------------
If I change line 16 to
16 throw (Base::RANGE_ERROR);
Then the error from the compiler becomes:
----------------------------------------
"x.C", line 16: Error: Operand expected instead of ";".
1 Error(s) detected.
----------------------------------------
I'm curious which, if any, of these expressions is actually
syntaxtically correct, according to the latest ANSI/ISO C++ working
paper.
Thanks for any help!
Doug
--
Douglas C. Schmidt
Department of Information and Computer Science
University of California, Irvine
Irvine, CA 92717. Work #: (714) 856-4105; FAX #: (714) 856-4056
Author: jjb@watson.ibm.com (John Barton)
Date: Wed, 13 Jul 1994 18:13:03 GMT Raw View
The short story:
C++ exceptions throw objects, Base::RANGE_ERROR is a struct,
therefore no syntax exists to throw Base::RANGE_ERROR.
You can throw an instance of the struct Base::RANGE_ERROR.
On way to create an instance is with a call to the constructor
to create a temp. Thus throw Base::RANGE_ERROR(); works.
John.
In article <2vvm8r$pm7@liege.ics.uci.edu>, schmidt@liege.ics.uci.edu (Douglas C. Schmidt) writes:
|> Hi,
|>
|> I'm trying to determine the proper syntax for throwing C++
|> exception expressions. The following code compiles and runs as
|> expected using SunC++ 4.0:
|>
|> ----------------------------------------
|> 1 #include <iostream.h>
|> 2
|> 3 class Base
|> 4 {
|> 5 public:
|> 6 struct RANGE_ERROR {
|> 7 RANGE_ERROR (int i = 10): j (i) {}
|> 8 int j;
|> 9 };
|> 10 };
|> 11
|> 12 class Derived : private Base
|> 13 {
|> 14 public:
|> 15 Derived (void) {
|> 16 throw Base::RANGE_ERROR ();
|> 17 }
|> 18
|> 19 Base::RANGE_ERROR;
|> 20 };
|> 21
|> 22 int main (void)
|> 23 {
|> 24 try
|> 25 {
|> 26 Derived d;
|> 27 }
|> 28 catch (Derived::RANGE_ERROR &e)
|> 29 {
|> 30 cout << "exception caught " << e.j << "!\n";
|> 31 }
|> 32 return 0;
|> 33 }
|> ----------------------------------------
|>
|> However, when line 16 is changed to
|>
|> 16 throw Base::RANGE_ERROR;
|>
|> The Sun C++ compiler complains:
|>
|> ----------------------------------------
|> "x.C", line 16: Error: Base::RANGE_ERROR may not be a type name.
|> "x.C", line 16: Error: Badly formed expression.
|> 2 Error(s) detected.
|> ----------------------------------------
|>
|> If I change line 16 to
|>
|> 16 throw (Base::RANGE_ERROR);
|>
|> Then the error from the compiler becomes:
|>
|> ----------------------------------------
|> "x.C", line 16: Error: Operand expected instead of ";".
|> 1 Error(s) detected.
|> ----------------------------------------
|>
|> I'm curious which, if any, of these expressions is actually
|> syntaxtically correct, according to the latest ANSI/ISO C++ working
|> paper.
|>
|> Thanks for any help!
|>
|> Doug
|> --
|> Douglas C. Schmidt
|> Department of Information and Computer Science
|> University of California, Irvine
|> Irvine, CA 92717. Work #: (714) 856-4105; FAX #: (714) 856-4056
--
John.
John J. Barton jjb@watson.ibm.com (914)784-6645
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598