Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 19th, 2008, 2:17 PM   #1
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 123
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
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?
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Jan 19th, 2008, 2:36 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,199
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Exception handling: point is...?

It seems appropriate to plug in an article by our resident member Grumpy, on Exceptions in C++.
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 2:39 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 652
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
Old Jan 19th, 2008, 2:54 PM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 655
Rep Power: 4 Jessehk is on a distinguished road
Re: Exception handling: point is...?

c++ Syntax (Toggle Plain Text)
  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)!

__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jan 19th, 2008, 3:14 PM   #5
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 123
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Exception handling: point is...?

Quote:
Originally Posted by Sane View Post
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)
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Jan 19th, 2008, 3:19 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,199
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Exception handling: point is...?

Quote:
Originally Posted by peaceofpi View Post
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 View Post
(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.
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 3:30 PM   #7
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 123
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Exception handling: point is...?

Quote:
Originally Posted by Sane View Post
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 View Post
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.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Jan 19th, 2008, 3:32 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,199
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Exception handling: point is...?

Glad my example helped. I gave you some rep. Look again.
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 4:11 PM   #9
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 123
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Exception handling: point is...?

Aww yeah!
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Jan 19th, 2008, 4:51 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,318
Rep Power: 5 grumpy will become famous soon enough
Re: Exception handling: point is...?

Quote:
Originally Posted by peaceofpi View Post
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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling and Events InfoGeek C# 3 Jan 9th, 2008 2:10 AM
exception handling class rwm C++ 5 Apr 10th, 2007 6:17 AM
exception handling l2u C++ 2 Mar 3rd, 2007 6:47 PM
Exception Handling with Threads Harakim Software Design and Algorithms 13 Aug 8th, 2006 7:00 AM
exception handling chrome_knob Java 2 May 19th, 2005 6:45 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:53 PM.

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