![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
hi: for(;;) goto hi;
|
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Re: Exception handling: point is...?
It seems appropriate to plug in an article by our resident member Grumpy, on Exceptions in C++.
|
|
|
|
|
|
#3 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4
![]() |
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";
}
} |
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
Re: Exception handling: point is...?
c++ Syntax (Toggle Plain Text)
6 1 Can't compute (-2)! ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#5 | |
|
hi: for(;;) goto hi;
|
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)
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
|
#6 | ||
|
Programming Guru
![]() |
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. |
||
|
|
|
|
|
#7 | ||
|
hi: for(;;) goto hi;
|
Re: Exception handling: point is...?
Quote:
Quote:
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
||
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
Re: Exception handling: point is...?
Glad my example helped. I gave you some rep. Look again.
![]() |
|
|
|
|
|
#9 |
|
hi: for(;;) goto hi;
|
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. |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Re: Exception handling: point is...?
Quote:
I am not a believer in spoon-feeding anyone except small babies or people who are somehow incapacitated. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |