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 ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Question Try...catch and XML

    Hey,

    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).

  2. #2
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Look up exceptions - you try the code you want to execute in the try block, and catch and deal with any exceptions within the catch block.

    I'm not an expert on exceptions, but if you do a search for exceptions you should be able to find more information.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    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

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    17
    Exceptions would prevent a blue screen yes. It would have the option to continue running instead of getting a blue screen.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Ohhh awesome!

    So if I was to write
    Code:
    try
    {
        char * mem = (char *) 0x0038490; // Pick any random location in memory
    }
    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!

    PS: Are these things standard C++, or are thein win32 only?

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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++.

    C++ uses keywords like try/catch/throw. Structured Exception Handling often uses __try/__except/__finally.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    Last edited by Cat; 07-06-2003 at 12:17 PM.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Exceptions can also be used to quickly exit a few functions at once. It's a hackish thing, though. I use it in my expression manipulator like so:
    Code:
    //warning: this code isn't tested, it's just a simplified version of my real code
    int main() {
    while (1) {
      std::string temp;
      getline(temp,std::cin);
      Expression e;
      try {
        e.parse(temp);
      }
      catch (const Exception& e) {
        std::cerr << e.str() << std::endl;
    
      }
      catch (const Function& f) {
        if (f.is_quit())
          return 0;
        if (f.is_help())
          std::cerr << "blah blah blah..." << std::endl;
      }
    }
    }
    If I found a quit function in my expression, I could throw it and handle any exit stuff at int main() as opposed somewhere in the parse() function or any of the functions it may call.

  10. #10
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    OMG... Moderators replied to my posts! I am truley blessed.

    Thanks guys, I think I understand exceptions now and when/not to use them Thankyou so much!

    (Psst... sorry to bug you again, but does anyone know anything about that XML?)

    ~ Paul

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Just FYI, I submitted a new FAQ on exceptions:
    http://faq.cprogramming.com/cgi-bin/...7&id=104478060

  12. #12
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    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 )

    Ps, that like you gave didn't work, it should be http://faq.cprogramming.com/cgi-bin/...&id=1044780608

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SAX XML Parser
    By magic.mike in forum C++ Programming
    Replies: 1
    Last Post: 12-24-2008, 09:52 AM
  2. Problem with Apache XML C++ Parser library
    By gandalf_bar in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2004, 10:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21