Could someone explain exception handling?

I've been wrestling with this for days now and I need to know it before Monday. And it's pissing me off that it's not getting stuck and just reading about it in my book grinds my gears because of the convoluted disgustingly stupid ... you get the point.

Someone please explain exactly what they're used for and what throw, try and catch do, and how they actually help you in a program. A very simple example should suffice, but I need a proper implementation example. Or something, I don't know, but what I don't need is some stupid explanation like "it's going to throw an exception, and then it will pass on that argument and it will then catch the exception" and bla bla effing bla...

Concrete examples, please. I'm sorry if I'm coming off as short but I find this chapter laughable considering how poorly written it is.
- What is exception handling used for?:
Exception Handling is used to watch for certain situations to occur that would change the flow of your program and allow you to enter conditions into your program to deal with these changes. An Example is kind of tough because we have so many options in C++ that I've never had to write one (except for hardware interupts), but from what I understand of them let's say your application is connecting to a remote system to do something, if that system passes your first test and says "Yup, I'm here" you application will continue executing but if for some reason it then stops responding then your next operation on it will fail and you wouldn't get much back in the way of an error. So instead of your program crashing you might ping it to see if it's still alive and if it is maybe you want to restart the remote service on that Host. Maybe you want it to send the remote host a WOL command in case it went into power save mode and wait a set amount of time? That would be a throw and a catch to call a function. And if that doesn't work how would you know? You would use try to execute the previous function then use throw to report the result and catch to deal with the next step, such as E mailing the Network Admin and dumping the current data to a log file.

- try: Tries something. This is probably less then helpful but read the others before dismissing me.

- throw: Is usually used in response to a condition, think of it like raising a flag to test for later with catch

- catch: Test to see if a "flag" was raised by throw

?- do: Possibly the intro of a do while loop? try, catch and throw are the three I've read about.

Here's a simple Psuedo-code sample for you: http://en.wikipedia.org/wiki/Exception_handling_syntax#C.2B.2B
Last edited on
Topic archived. No new replies allowed.