J
junw2000
In the following code:
#include <iostream>
#include <exception>
using std::string;
using std::exception;
class STACK {
public:
STACK(): size(0){
str = new string[MAX];
}
void push(string s){
try{
if(size == MAX){
throw 10;
}
str[size++] = s;
}
catch(int){
std::cout<<"stack is full."<<std::endl;
throw "A"; // LINE1
}
}
string pop(){
if(size == 0){
std::cout<<"stack is empty"<<std::endl;
return " ";
}
return str[--size];
}
bool empty(){
return size == 0;
}
private:
enum { MAX = 3};
string *str;
int size;
};
int main(){
string ss;
STACK stack1;
stack1.push("hello");
stack1.push("you");
stack1.push("he");
stack1.push("it");
/* LINE2
catch(...){
std::cout<<"rethrow."<<std::endl;
}
*/
}
At LINE1, I re-throw an exception. How to catch it?
I tried to use the catch block at LINE2, but it cause error: syntax
error before `catch'
Thanks.
Jack
#include <iostream>
#include <exception>
using std::string;
using std::exception;
class STACK {
public:
STACK(): size(0){
str = new string[MAX];
}
void push(string s){
try{
if(size == MAX){
throw 10;
}
str[size++] = s;
}
catch(int){
std::cout<<"stack is full."<<std::endl;
throw "A"; // LINE1
}
}
string pop(){
if(size == 0){
std::cout<<"stack is empty"<<std::endl;
return " ";
}
return str[--size];
}
bool empty(){
return size == 0;
}
private:
enum { MAX = 3};
string *str;
int size;
};
int main(){
string ss;
STACK stack1;
stack1.push("hello");
stack1.push("you");
stack1.push("he");
stack1.push("it");
/* LINE2
catch(...){
std::cout<<"rethrow."<<std::endl;
}
*/
}
At LINE1, I re-throw an exception. How to catch it?
I tried to use the catch block at LINE2, but it cause error: syntax
error before `catch'
Thanks.
Jack