- 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