0

I have to make a try...catch in this function but i don't know how to continue the program: Display the array after catch an exception by displaying a[size].

Please help me.

Sorry for my bad English.

Here is my program... Thank you so much...

#include <iostream>
using namespace std;

namespace arr{
    template<class T>
    class Array{
        private:
            int size;
            T* a;
        public:
            Array<T>();
            Array<T>(int max);
            ~Array<T>();
            T& operator[](int index) throw(int);
            T& operator=(const T& a2);
            void set(int index, T val);
            void Display();
            int GetSize();
            void Sort();
            T Max();
    };
}

using namespace arr;

template<class T>
Array<T>::Array(){
    size=0;
    a=new T[0];
}

template<class T>
Array<T>::Array(int max){
    size=max;
    a=new T[size];
}

template<class T>
Array<T>::~Array(){
    size=0;
    delete[] a;
}

template<class T>
T& Array<T>::operator[](int index) throw(int){
    try{
        if(index>=0 && index <size)
            return a[index];
        else
            throw(index);
    } catch(int e){
        cout<<"Exception Occured: ";
        if(e<0)
            cout<<"index < 0\n";
        else
            cout<<"index >= "<<size<<endl;
        exit(1);
    }
}

template<class T>
T& Array<T>::operator=(const T& a2){
    for(int i=0;i<a2.GetSize();i++) set(i,a2[i]);
    return *this;
}

template<class T>
void Array<T>::set(int index, T val){
    a[index]=val;
}

template<class T>
void Array<T>::Display(){
    for(int i=0;i<size;i++)
        cout<<a[i]<<" ";
    cout<<endl;
}

template<class T>
int Array<T>::GetSize(){
    return size;
}

template<class T>
void Array<T>::Sort(){
    for(int i=0;i<size-1;i++)
        for(int j=i+1;j<size;j++)
            if(a[i]>a[j]){
                T tmp=a[i];
                a[i]=a[j];
                a[j]=tmp;
            }
}

template<class T>
T Array<T>::Max(){
    T m=a[0];
    for(int i=1;i<size;i++)
        if(a[i]>m) m=a[i];
    return m;
}

int main(){
    int size=0;
    cout<<"Size of Array: ";
    cin>>size;
    Array<int> a(size);
    for(int i=0;i<size;i++){
        int tmp;
        cout<<"a["<<i+1<<"]=";
        cin>>tmp;
        a.set(i,tmp);
    }
    cout<<a[size]<<endl; //Test Exception.
    cout<<"Your Array:\n";
    a.Display();
    return 0;
}
aschepler
  • 70,891
  • 9
  • 107
  • 161
luvasa
  • 3
  • 1
  • 6
  • You should work on supplying a copy constructor for your class. And also, why in the world are you closing down the entire app because the index is out of bounds? http://stackoverflow.com/questions/22843189/exit-call-inside-a-function-which-should-return-a-reference – PaulMcKenzie Apr 30 '15 at 03:08
  • i don't know what to do.. could you please give some instructors? – luvasa Apr 30 '15 at 03:12
  • `I don't know what to do`. See the link I posted in my comment. Don't intentionally shut down an application like that in the middle of class code. All you need to do is `throw`, and let the client code be responsible for catching the error and doing whatever they need to do. In other words, don't be a bully by shutting down everything, just because I gave your class a bad index. – PaulMcKenzie Apr 30 '15 at 03:13
  • `You should work on supplying a copy constructor for your class` I just don't understand this sentence. i don't know what function in the class you're mentioning. – luvasa Apr 30 '15 at 03:18
  • `Array(const Array&);` You are missing that function. If you don't have that function, you can't create an `Array` from an existing `Array` instance without invoking undefined behavior. Look up "the rule of 3". – PaulMcKenzie Apr 30 '15 at 03:19
  • oh, I've read what you add to your comment. :) – luvasa Apr 30 '15 at 03:21
  • @PaulMcKenzie if i declared the function as: `T& operator[](int index);` , how can i define the function by using a `try...catch` block inside? – luvasa Apr 30 '15 at 04:34
  • 1
    There is no need for the `try...catch` inside of your class. Just issue a `throw`. An array class shouldn't be in the business of direct error handling. All it should do is let the caller know that something went wrong, and you do that by issuing the `throw` and let the caller handle any issues. – PaulMcKenzie Apr 30 '15 at 09:36

1 Answers1

0

When you have a function declared as:

T& operator[](int index) throw(int);

it is expected to throw an exception. It is not required to have a try/catch block to throw an exception and deal with the exception.

Your implementation can be:

template<class T>
T& Array<T>::operator[](int index) throw(int){

   // Check whether the index is valid.
   // If it is not, throw an exception.
   if(index < 0  || index >= size)
      throw index;

   // The index is valid. Return a reference
   // to the element of the array.
   return a[index];
}

Users of the function will be required to use a try/catch block.

Array<int> a;
try 
{
   int e = a[5]; // This call will throw an exception.
}
catch (int index)
{
   // Deal with out of bounds index.
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • i did it like that before but if i declared the function as: `T& operator[](int index);` so, how can i define the function by using a `try...catch` block inside? – luvasa Apr 30 '15 at 03:23