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, 3:59 PM   #11
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Exception handling: point is...?

Quote:
Originally Posted by grumpy View Post
I am not a believer in spoon-feeding anyone except small babies or people who are somehow incapacitated.
But not if you're the cause of their incapacitation, right?
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 4:06 PM   #12
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Re: Exception handling: point is...?

While I have the capability to do so, I have never incapacitated anyone's ability to learn, and hopefully will never have to.
grumpy is offline   Reply With Quote
Old Jan 19th, 2008, 5:28 PM   #13
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 259
Rep Power: 3 Cache is on a distinguished road
Re: Exception handling: point is...?

Channel 9 did an interview with Ale Contenti on exception handling. People here might find it interesting.

Understanding Exceptions and When/How to Handle Them:
http://channel9.msdn.com/Showpost.aspx?postid=343189

I was surprised to learn about the amount of bookkeeping that goes on under the covers when using exceptions.
Cache is offline   Reply With Quote
Old Jan 20th, 2008, 3:44 PM   #14
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 86
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 grumpy View Post
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.
I never said I wanted to be spoonfed, and I'll tell you now I still don't want it. Honestly, as someone who knows as little about exceptions as I do, the article went far over my head pretty quickly. I suppose that's my problem, but it certainly doesn't mean I'm part of the herd of people here with low post counts and high expectations.
__________________
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 20th, 2008, 11:05 PM   #15
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 245
Rep Power: 1 Jabo is on a distinguished road
Re: Exception handling: point is...?

From what I know about error handling (which is pretty slim) it could be more robust. Let me use an example. If you're capturing keyboard input in your program, you poll for whatever keystrokes and if found, you mark it as handled, otherwise it goes on to the OS to be handled. Error handling, from my recollection, doesn't have this function. Why?

The error handling mechanism automatically handles every error, even if it doesn't? That seems broken.
Jabo is offline   Reply With Quote
Old Jan 21st, 2008, 9:57 AM   #16
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 86
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...?

How's this for a first attempt?

c++ Syntax (Toggle Plain Text)
  1. bool isprime(int n)
  2. {
  3. try
  4. {
  5. if (n == 0) throw 0;
  6. if (n < 0) throw -1;
  7. if (n % 2 == 0) return false;
  8. if (n == 1 || n == 2) return true;
  9. for (int i = 3; i < n/2; i++)
  10. if (n % i == 0)
  11. return false;
  12. }
  13. catch(int i)
  14. {
  15. // handle 0 or negative numbers here
  16. }
  17. return true;
  18. }
__________________
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 21st, 2008, 2:47 PM   #17
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Re: Exception handling: point is...?

That code would be thrown out in any serious code review, and the author sacked on the spot.

There is no practical need to throw/catch an exception if a function that that detects an error can recover from it. It would be simpler to just recover on the spot ..... and return false. The code is simpler, easier to understand, and probably (depending on how exception handling is implemented by your compiler) more efficient.

0,1, and negative values are, by definition, non-prime (yes, your algorithm is incorrect as it returns true for 1) so the error recovery is trivial. I won't even address that fact that, efficiency wise, the loop is .... bleh!
grumpy is offline   Reply With Quote
Old Jan 21st, 2008, 3:08 PM   #18
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Exception handling: point is...?

Yikes! Your function says 1 is a prime number, and 2 is composite!
Sane is offline   Reply With Quote
Old Jan 22nd, 2008, 1:40 PM   #19
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 86
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...?

Factors of 2: 1, 2, ?

As for grumpy
Quote:
Originally Posted by grumpy
That code would be thrown out in any serious code review
Quote:
Originally Posted by me
How's this for a first attempt?
I got my answer, I guess I should have known better than to ask further questions.

Regardless,
c++ Syntax (Toggle Plain Text)
  1. bool isprime(int n)
  2. {
  3. if (n <= 1) return false;
  4. if (n == 2) return true; // stays till someone comes up with another factor of 2
  5. for (int i = 3; i <= n/2; i++)
  6. if (n % i == 0)
  7. return false;
  8. return true;
  9. }
__________________
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 22nd, 2008, 3:19 PM   #20
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Exception handling: point is...?

I can't tell if you were agreeing or disagreeing with me, but either way, your new code handles n=2 properly now. Your old code returned false when n % 2 == 0. That was incorrect, since 2 is a prime number.

Anyways, you have it now.
Sane 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 1:10 AM
exception handling class rwm C++ 5 Apr 10th, 2007 5:17 AM
exception handling l2u C++ 2 Mar 3rd, 2007 5:47 PM
Exception Handling with Threads Harakim Software Design and Algorithms 13 Aug 8th, 2006 6:00 AM
exception handling chrome_knob Java 2 May 19th, 2005 5:45 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:01 AM.

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