How Exceptions are Used - C++ Statement

C++ examples for Statement:try catch

Introduction

A try block is created to surround areas of code that might have a problem and throw an exception.

For example:

 
try { 
   someDangerousFunction(); 
} 
 

A catch block is the block immediately following a try block in which exceptions are handled.

For example:

 
try { 
   someDangerousFunction(); 
} catch (outOfMemory) { 
   // take action to recover from low memory condition 
} catch (fileNotFound) { 
   // take action when a file is not found 
} 
 

Demo Code

                                                            
#include <iostream> 
                                                              
const int defaultSize = 10; 
                                                              
// define the exception class 
class XBoundary //from   www.  j a  v a  2s .co m
{ 
public: 
    XBoundary() {} 
    ~XBoundary() {} 
private: 
}; 
                                                              
class Array 
{ 
public: 
    // constructors 
    Array(int size = defaultSize); 
    Array(const Array &rhs); 
    ~Array() { delete [] pType; } 
                                                              
    // operators 
    Array& operator=(const Array&); 
    int& operator[](int offSet); 
    const int& operator[](int offSet) const; 
                                                              
    // accessors 
    int getSize() const { return size; } 
                                                              
    // friend function 
    friend std::ostream& operator<<(std::ostream&, const Array&); 
private: 
    int *pType; 
    int size; 
}; 
                                                              
Array::Array(int newSize): 
size(newSize) 
{ 
    pType = new int[size]; 
    for (int i = 0; i < size; i++) 
         pType[i] = 0; 
} 
                                                              
Array& Array::operator=(const Array &rhs) 
{ 
    if (this == &rhs) 
         return *this; 
    delete [] pType; 
    size = rhs.getSize(); 
    pType = new int[size]; 
    for (int i = 0; i < size; i++) 
         pType[i] = rhs[i]; 
    return *this; 
} 
                                                              
Array::Array(const Array &rhs) 
{ 
    size = rhs.getSize(); 
    pType = new int[size]; 
    for (int i = 0; i < size; i++) 
         pType[i] = rhs[i]; 
} 
                                                              
int& Array::operator[](int offSet) 
{ 
    int size = getSize(); 
    if (offSet >= 0 && offSet < size) 
         return pType[offSet]; 
    throw XBoundary(); 
    return pType[offSet]; 
} 
                                                              
const int& Array::operator[](int offSet) const 
{ 
    int size = getSize(); 
    if (offSet >= 0 && offSet < size) 
         return pType[offSet]; 
    throw XBoundary(); 
    return pType[offSet]; 
} 
                                                              
std::ostream& operator<<(std::ostream& output, 
                         const Array& array) 
{ 
    for (int i = 0; i < array.getSize(); i++) 
            output << "[" << i << "] " << array[i] << "\n"; 
    return output; 
} 
                                                              
int main() 
{ 
    Array intArray(20); 
    try 
    { 
            for (int j = 0; j < 100; j++) 
            { 
                 intArray[j] = j; 
                 std::cout << "intArray[" << j 
                      << "] OK ..." << "\n"; 
            } 
    } 
    catch (XBoundary) 
    { 
            std::cout << "Unable to process your input\n"; 
    } 
    std::cout << "Done\n"; 
    return 0; 
}

Result


Related Tutorials