Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Exception handling: point is...? (http://www.programmingforums.org/showthread.php?t=14979)

peaceofpi Jan 19th, 2008 2:17 PM

Exception handling: point is...?
 
I've read through it in every C++ tutorial I've tried, and it just doesn't make sense. Why use it?

Can someone give an example to demonstrate a real use for try, catch, and throw?

Sane Jan 19th, 2008 2:36 PM

Re: Exception handling: point is...?
 
It seems appropriate to plug in an article by our resident member Grumpy, on Exceptions in C++.

Ancient Dragon Jan 19th, 2008 2:39 PM

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";
    }
}


Jessehk Jan 19th, 2008 2:54 PM

Re: Exception handling: point is...?
 
:

  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. #include <boost/format.hpp> // for easy string building
  5.  
  6. int factorial( int n ) {
  7.     if ( n < 0 ) {
  8.         using namespace boost;
  9.         format errmsg( "Can't compute (%d)!" );
  10.         throw std::invalid_argument( (errmsg % n).str() );
  11.     }
  12.  
  13.     int result = 1;
  14.     while ( n != 0 )
  15.         result *= n--;
  16.  
  17.     return result;
  18. }
  19.  
  20. int main() {
  21.     try {
  22.         std::cout << factorial( 3 ) << std::endl;
  23.         std::cout << factorial( 0 ) << std::endl;
  24.         std::cout << factorial( -2 ) << std::endl;
  25.     } catch ( std::invalid_argument &e ) {
  26.         std::cerr << e.what() << std::endl;
  27.     }
  28. }


:

6
1
Can't compute (-2)!


:)

peaceofpi Jan 19th, 2008 3:14 PM

Re: Exception handling: point is...?
 
Quote:

Originally Posted by Sane (Post 139903)
It seems appropriate to plug in an article by our resident member Grumpy, on Exceptions in C++.

Yes, I've read it before and just read it again, but it seems tailored to people who already have a good understanding of exceptions.

As for Ancient Dragon and Jessehk: From your examples, exception handling seems to be useful for properly exiting functions should something go wrong. I think I'm beginning to see the uses but it might still be a while before something hits me on the head and shows me the real power of them (like the trouble I had pointers a while ago).

(Off topic: Is there any way to find out who added to your reputation and why? I've got 2 rep power but I don't remember being particularly helpful at all)

Sane Jan 19th, 2008 3:19 PM

Re: Exception handling: point is...?
 
Quote:

Originally Posted by peaceofpi (Post 139907)
I think I'm beginning to see the uses but it might still be a while before something hits me on the head and shows me the real power of them (like the trouble I had pointers a while ago).

As I understand, it's not as much about 'power' as it is about 'simplicity' and 'stability'. It's used when you want to leave a program's behaviour in the event of invalid input or invalid operation up to the responsibility of the calling routine. This yields some very useful properties when you're working with large programs with complicated infrastructures.

Edit
For instance, take Jessehk's factorial function. If you were writing a 'math tutoring' program, there might be several ways you use the factorial function. You might use it to check answers for randomly generated questions. You could also use it as a part of a calculator.

If it's for checking an answer, in the event of an exception, you could just simply state that the answer was incorrect. However, this isn't the same way you want to handle the exception if it's a calculator. You might want to pop up a dialog box saying you can't take the factorial of negative numbers.

So with exceptions, you can use the same function for a wide array of purposes, without limiting its functionality.

Quote:

Originally Posted by peaceofpi (Post 139907)
(Off topic: Is there any way to find out who added to your reputation and why? I've got 2 rep power but I don't remember being particularly helpful at all)

Control Panel. First thing you see there.
You might have the normal amount of rep for someone who's been here almost 2 years.

peaceofpi Jan 19th, 2008 3:30 PM

Re: Exception handling: point is...?
 
Quote:

Originally Posted by Sane (Post 139908)
As I understand, it's not as much about 'power' as it is about 'simplicity' and 'stability'. It's used when you want to leave a program's behaviour in the event of invalid input or invalid operation up to the responsibility of the calling routine. This yields some very useful properties when you're working with large programs with complicated infrastructures.

Edit
For instance, take Jessehk's factorial function. If you were writing a 'math tutoring' program, there might be several ways you use the factorial function. You might use it to check answers for randomly generated questions. You could also use it as a part of a calculator.

If it's for checking an answer, in the event of an exception, you could just simply state that the answer was incorrect. However, this isn't the same way you want to handle the exception if it's a calculator. You might want to pop up a dialog box saying you can't take the factorial of negative numbers.

So with exceptions, you can use the same function for a wide array of purposes, without limiting its functionality.

Thanks, surprisingly that puts it into perspective for me. I will see if I can implement try/catch/throw into any new projects and that will probably help further, I can already think of some places it'd be good in.

Quote:

Originally Posted by Sane (Post 139908)
Control Panel. First thing you see there.
You might have the normal amount of rep for someone who's been here almost 2 years.

That's where I expected it to be, but there's nothing mentioning rep at all.

Sane Jan 19th, 2008 3:32 PM

Re: Exception handling: point is...?
 
Glad my example helped. I gave you some rep. Look again. ;)

peaceofpi Jan 19th, 2008 4:11 PM

Re: Exception handling: point is...?
 
Aww yeah!

grumpy Jan 19th, 2008 4:51 PM

Re: Exception handling: point is...?
 
Quote:

Originally Posted by peaceofpi (Post 139907)
Yes, I've read it before and just read it again, but it seems tailored to people who already have a good understanding of exceptions.

No, it is not. It does, however, rely on the reader being willing to put some effort into understanding rather than being provided with a set of simple recipes they can apply without thought.

I am not a believer in spoon-feeding anyone except small babies or people who are somehow incapacitated.


All times are GMT -5. The time now is 3:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC