heritaged exception features

E

eric

Dear advanced c++ programers:

I tried to copied and test a piece of (simple code) from page 823 of
book (C++ Primer Plus-5th Ed by Stephen Prata). about heritaged
exceptions features.

--------------------------------------
#include <iostream>

using namespace std;

class bad_1 {...};
class bad_2 : public bad_1 {...};
class bad_3 : public bad_2 {...};

void duper() throw (bad_1) // matches base- and derived-class objects
{
int oh_no = 1,
rats = 0,
drat = 0;

if (oh_no)
throw bad_1();
if (rats)
throw bad_2();
if (drat)
throw bad_3();

}

int main() {
try {
duper();
}

catch(bad_3) {
cout << "\ngoto bad_3\n";
}

catch(bad_2) {
cout << "\ngoto bad_2\n";
}

catch(bad_1) {
cout << "\ngoto bad_1\n";
}

}
----------------------------------------------
but I got compile error:

eric@eric-laptop:~/cpppp5/ch15$ g++ pg823.cpp
pg823.cpp:5:14: error: expected unqualified-id before ‘...’ token
pg823.cpp:6:29: error: expected unqualified-id before ‘...’ token
pg823.cpp:7:29: error: expected unqualified-id before ‘...’ token

plz help , see how to modify (or what kind (unqualified-id) I should
add in somewhere in my simple test program)?
thanks a lot in advance, Eric
 
V

Victor Bazarov

I tried to copied and test a piece of (simple code) from page 823 of
book (C++ Primer Plus-5th Ed by Stephen Prata). about heritaged
exceptions features.

--------------------------------------
#include<iostream>

using namespace std;

class bad_1 {...};
class bad_2 : public bad_1 {...};
class bad_3 : public bad_2 {...};

Are those ellipses (sets of dots) in your source code *verbatim*? The
example in the book simply didn't want to elaborate on the contents of
those classes (likely), and you have to at least remove those.
Otherwise, it's a simple syntax error. Change those three lines to read:

class bad_1 {};
class bad_2 : public bad_1 {};
class bad_3 : public bad_2 {};
void duper() throw (bad_1) // matches base- and derived-class objects
{
int oh_no = 1,
rats = 0,
drat = 0;

if (oh_no)
throw bad_1();
if (rats)
throw bad_2();
if (drat)
throw bad_3();

}

int main() {
try {
duper();
}

catch(bad_3) {
cout<< "\ngoto bad_3\n";
}

catch(bad_2) {
cout<< "\ngoto bad_2\n";
}

catch(bad_1) {
cout<< "\ngoto bad_1\n";
}

}
----------------------------------------------
but I got compile error:

eric@eric-laptop:~/cpppp5/ch15$ g++ pg823.cpp
pg823.cpp:5:14: error: expected unqualified-id before ‘...’ token
pg823.cpp:6:29: error: expected unqualified-id before ‘...’ token
pg823.cpp:7:29: error: expected unqualified-id before ‘...’ token

plz help , see how to modify (or what kind (unqualified-id) I should
add in somewhere in my simple test program)?
thanks a lot in advance, Eric

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top