Try, Catch, Throw?

Started by
4 comments, last by ahmed30 15 years, 3 months ago
Ok, I've been learning C++ slowly and there is always something new that pops up. Usually I have a reference book or something so I can check it out and see what the code does, but today I ran across something I've never even heard of: try, catch and throw. To stop myself from looking like an idiot I'm not going to try to explain what I think these are for. I looked online for some guides but, well... Could someone dumb that down for me? :D
--------- ApochPiQ : <Serious Grammar Nazi Pet Peeve> FFS guys, it's spelled "dying". That is all. </Serious Grammar Nazi Pet Peeve>
Advertisement
Consider an object representing an XML document. That object has a constructor that takes a string argument and parses that string into an XML document.

What happens if the string document doesn't represent a valid document? What does the final object look like?

You can't use a "default" object because that would cause incorrect behavior (you want to be able to detect errors when they happen) and you can't use an "error" state for the object (or you would have to check for validity every time you use the object, not only at creation time). The solution is to prevent the construction of an object altogether.

How? Throw an exception. That exception propagates in the program, up the call stack, until it reaches a try-catch block that matches its type. Once it's reached, all the stack-allocated data allocated since the block started is released in order, and the construction of any objects is canceled. The execution then resumes in the catch block.
To simplify slightly:
A try block defines a region of code that you wish to monitor for exceptions. It must be followed by one or more catch clauses. Each catch clause provides a description of the type of exception it handles, and it is possible to write a clause for "all exceptions." Any exceptions that are not handled move on up the call stack - they propagate.

try{  // run code that might generate exceptions}catch(ExceptionType1 &e){  // do something if an exception of ExceptionType1 is throw inside of our try block}catch(ExceptionType2 &e){  // do something if an exception of ExceptionType2 is throw inside of our try block}catch(...){  // handle ALL other types of exceptions}


A throw statement is used to create an exception. If something goes wrong and you want some other piece of code that may have called yours to take care of it, you throw an exception, and it propagates upward to the matching catch (see? throw, catch?)

Note that unhandled exceptions propagate all the way to the OS, with the program terminating as a consequence.
Well that's just awesome. Thanks a ton for breaking that down for me.
--------- ApochPiQ : <Serious Grammar Nazi Pet Peeve> FFS guys, it's spelled "dying". That is all. </Serious Grammar Nazi Pet Peeve>
exception handling ....


its one of the bits of C++ that makes 'GOTOs' look elegant (the excuse they usually give for not using GOTOs...)

The mechanism is usefull if you can use it systematically.
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
the try and catch are to handle errors and exceptions
and to prevent errors that crash the program

such as upload an invalid file , divide by zero etc.

so you put the code that you want to make sure that it contains no error in "try" , then put the solution taken in "catch ".. such as an error msg appears .

Another thing to keep in mind is when you use the try and catch in a funtion , this function must throw Exception or IOException ..



This topic is closed to new replies.

Advertisement