Cpp - Exception Handling Introduction

Introduction

C++ exception handling provides a type-safe technique for dealing with the predictable but unusual conditions that arise while running a program.

A try block wraps areas of code that might have a problem and throw an exception. For example:

try { 
   someDangerousFunction(); 
} 

A catch block immediately follows 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 
} 

When an exception is thrown, control transfers to the catch block immediately following the current try block.

Demo

#include <iostream> 
 
const int defaultSize = 10; 
 
class ArrayIndexOfBoundException { 
public: //w w w .j  a v a 2s  .  com
    ArrayIndexOfBoundException() {} 
    ~ArrayIndexOfBoundException() {} 
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 ArrayIndexOfBoundException(); 
    return pType[offSet]; 
} 
 
const int& Array::operator[](int offSet) const 
{ 
    int size = getSize(); 
    if (offSet >= 0 && offSet < size) 
         return pType[offSet]; 
    throw ArrayIndexOfBoundException(); 
    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 (ArrayIndexOfBoundException) 
    { 
            std::cout << "Unable to process your input\n"; 
    } 
    std::cout << "Done\n"; 
    return 0; 
}

Result