Try...catch and XML

This is a discussion on Try...catch and XML within the C++ Programming forums, part of the General Programming Boards category; Hey,
I'm trying to learn how to parse an XML document so I downloaded an example from MSDN, and I ...

Try...catch and XML

I'm trying to learn how to parse an XML document so I downloaded an example from MSDN, and I saw it had something like this:

Code:

try
{
// Some code...
}
catch (SOMETHING)
{
// More code...
}

I was just wondering what try/catch does, and how I would go about using them. I've never seen them in any of the C++ books I've read or in any other examples.

PS: Also, if anyone knows of any really easy to use XML parsers I could use to just read the values from a small document I would really appreciate a link to them, because this MSXML seems much too long and complicated for what I want to do (just like the rest of the WINAPI really).

Hey, thanks very much. I looked through a few examples given and wrote one of my own to test and it worked fine, I think I understand them.

So how often should I use exceptions? Why wouldn't I just use a return value to report an error?

Also, if I used try..catch with a function that might be..... say, an invalid pointer for instace (which would normally give me a bluescreen or something), would it just go through it normally without actually forcing me to restart?

lol, and any thoughts on the XML? There seems so much overhead that I'm thinking I might just write my own functions

There would be no bluescreen? (Assuming of course that the code above would generate one, I don't intend to try) Even if there was no exeptions thrown? Or, if say I knew the execption generated (maybe a General Protection Fault) I could just say catch (general_protection_fault) and everything would be fine?

Wow, these things are so cool, I can't believe they're not in any of my books!

In windows, there's a type of exception handling that does catch exceptions relating to access violations and such (called structured exception handling), but they are nothing to do with the exception handling that is part of C++.

Originally posted by stovellp
So how often should I use exceptions? Why wouldn't I just use a return value to report an error?

Well, one good reason is that exceptions let you decide where you want to handle the error. Rather than having to handle it immediately, you could handle it elsewhere.

E.g. say you have a function A(). A() calls B() which calls C() which calls D(). If D() returned an error, you'd have to either handle it in C() or add additional code into C() to pass the error up. If D() threw an exception, it would automatically propagate up (with no additional code), so A() could handle it by simply enclosing its call to B() in a try...catch.

Further, return codes for errors can be ignored; an exception must be handled. If your code won't handle it, the runtime will handle it, usually by displaying a message to the user and terminating. Say some operation fails and the data has become corrupted. If you return an error code, the program could well ignore it and keep using the data. Be honest, do you always remember to check every single return code every time?

Also, you may not have a return type that can easily be used as an error code. For example, if you have a function that returns bool, there ARE only 2 potential return values.

It's also possible now to avoid having to check every return value. Say you had a dozen functions in a row, each of which could fail, and any failure should display a message and end the program. It would be much less duplication of effort to use exceptions instead of return codes.

One caveat: destructors should never, ever throw an exception. If your destructor calls a function that throws, the destructor must catch the exception.

Additionally, say you had an add() function. Its job is of course to add two integers, but if it fails, and has to return an error code, what value does it return. Every possibility is a valid output of a run with no error, so perhaps you pass the error checking value as a pointer to the function (which, in practice will just be easier to pass a null pointer and forget about the error status).

Exceptions unwind the call stack until caught. This also can prevent invalid structures from being formed (there was a thread about constructors and exceptions a few days ago you might want to look at).

Awesome! I'm rather annoyed that all the books I've gotten from my Uni actually have nothing about namespaces or exceptions, so that FAQ will be very useful to me (considering I'll forget everything I've just learned in a matter of weeks )