Search code examples
c++exceptionstackthrow

Throws keyword in C++


Can someone point the right way to do this program with exception handling? It is a program for stacks.

1)#include<iostream>
2)#include<stdlib.h>
3)#include<string.h>
4)using namespace std;
5)
6)template<class T>
7)class Stack {
8)private:
9)  int max;
10) int top;
12) T* items;
13)public:
14) Stack(int size) {
15)     max = size;
16)     top = -1;
17)     items = new T[max];
18) }
19) ~Stack(){delete [] items;}
20)
21) void push(T data) throws Exception{
22)     if(full()) {
23)         throw new StackException("Out of space!");
24)     }
25)     items[++top] = data;
26) }
27) T pop(){
28)     if(empty()) throws Exception {
29)         throw new StackException("No more elements to delete"); 
30)     }
31)     return items[top--];
32) }   
33) 
34) bool full() { return top == max-1; }
35) bool empty() { return top == -1; }
36)};
37)
38)int main() {
39)    try{
40)     Stack<int> s(10);
41)     s.push(1);
42)     s.push(2);
43)     cout<<s.pop()<<endl;
44)    } 
45)    catch(StackException e){
46)     cout<<e.what()<<endl;
47)    }
48)    return 0;
49)}

Edit : I am getting the following errors. I am new to Exception Handling in C++ , wanted to know if I am doing right --

     3stacks.cpp:20:18: error: expected ‘;’ at end of member declaration
     3stacks.cpp:20:20: error: ‘throws’ does not name a type
     3stacks.cpp:26:8: error: expected ‘;’ at end of member declaration
     3stacks.cpp:26:10: error: ‘throws’ does not name a type 
     3stacks.cpp: In function ‘int main()’:
     3stacks.cpp:44:8: error: expected type-specifier before ‘StackException’
     3stacks.cpp:44:23: error: expected ‘)’ before ‘e’
     3stacks.cpp:44:23: error: expected ‘{’ before ‘e’
     3stacks.cpp:44:23: error: ‘e’ was not declared in this scope
     3stacks.cpp:44:24: error: expected ‘;’ before ‘)’ token

Solution

  • You haven't defined StackException anywhere in your program. You have to create it yourself. Also strip throws Exception from your function signature, since you never defined that type either (and it's called throw Exception).

    Furthermore it isn't really necessary to state what exceptions are possible in the signature, but it's better to state that a function will never throw (using noexcept in C++11). State possible exceptions in the documentation. Furthermore, you missed a possible bad_alloc.

    All in all, strip all your code and use std::stack from <stack> and strip those C libraries. However, here is a example how you could do it:

    template<class T>
    class Stack {
    private:
        int max;
        int top;
        T * items;
    public:
        struct out_of_space{};
        struct empty_stack{};
        Stack(int size) {
            if(size)
                max = size;
            top = -1;
            items = new T[max];
        }
        ~Stack(){delete[] items;}
    
        void push(const T & data){
            if(full()) {
                throw out_of_space();
            }
            items[++top] = data;
        }
        T pop(){
            if(empty()){
                throw empty_stack();
            }
            return items[top--];
        }   
    
        bool full() const { return top == max-1; }
        bool empty() const { return top == -1; }
    };
    
    int main() {
        try{
         Stack<int> s(10);
         s.push(1);
         s.push(2);
         cout<<s.pop()<<endl;
        } catch(const Stack<int>::out_of_space& e){
         cout<< "stack out of space" <<endl;
        } catch(const Stack<int>::empty_stack & e){
         cout<< "stack is empty" <<endl;
        }
        return 0;
    }
    

    To actually use e.what() you would have to implement it yourself. Or you could inherit std::exception, overload it and just catch const std::exception&.