![]() |
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? |
Re: Exception handling: point is...?
It seems appropriate to plug in an article by our resident member Grumpy, on Exceptions in C++.
|
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 mainHere is another -- the new operator throws an exception if memory allocation error occurs (such as out of memory) :
int main() |
Re: Exception handling: point is...?
:
:
6:) |
Re: Exception handling: point is...?
Quote:
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) |
Re: Exception handling: point is...?
Quote:
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:
You might have the normal amount of rep for someone who's been here almost 2 years. |
Re: Exception handling: point is...?
Quote:
Quote:
|
Re: Exception handling: point is...?
Glad my example helped. I gave you some rep. Look again. ;)
|
Re: Exception handling: point is...?
Aww yeah!
|
Re: Exception handling: point is...?
Quote:
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