exception

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
 
V

Victor Bazarov

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(...){

OK, it's a 'catch' block. Where is the 'try' block for it?
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'

You need to add 'try' before it. Right now it's like an "else"
without the corresponding "if"...

V
 
M

Marcus Kwok

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'

A "catch" block must come immediately after a "try" block. So, you must
enclose your push() statements in a "try" block.

Also, to be correct you need to also #include <string>.
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top