View Single Post
Old Jan 19th, 2008, 1:39 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Exception handling: point is...?

[Edit]I didn't see Sane's post when I wrote this. Grumpy's article has a lot more usefule information than my examples below [/edit]

Here is an simple example: suppose you want to divide one number by another but you are concerned that divide-by-zero error may occur
int main
{
    int a = 10;
    int b = 0;
    int c = 0;
    try
    {
          c = a / b;
    }
    catch(...)
    {
         cout << "Division by zero\n";
    }
}

Here is another -- the new operator throws an exception if memory allocation error occurs (such as out of memory)
int main()
{
    int * array;
    try
    {
        array = new int[10000000];
    }
    catch(...)
    {
          cout << "Oops!\n";
     }
}
Ancient Dragon is offline   Reply With Quote