Jump to content
  • Advertisement
Sign in to follow this  
Dark_Glitch

Try, Catch, Throw?

This topic is 3586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ..



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!