How do I handle RapidXml errors?

ransh

RapidXml throws an exception in case of an invalid XML file. Is it possible to recover from such a failure?

For example, is it possible to check if the XML is valid beforehand, or recover and continue on?

It seems that when such failures happen, there is only assert and exit of process, and no chance for recovery.

softwariness

By default, RapidXML raises exceptions on parse errors; it doesn't assert (perhaps by assert you just meant the process aborts).

It is possible to configure RapidXML with your own error handler called rapidxml::parse_error_handler if you #define RAPIDXML_NO_EXCEPTIONS before including the RapidXML headers, and if such an error handler returns, RapidXML will call assert(0), but I suspect that you don't have that enabled and you just need to be catching the right exception.

There's just one exception to catch for parse errors, and it's called rapidxml::parse_error, but RapidXML will also throw std::runtime_error if it fails to find the file.

Here's an example which catches both exception types, plus some catch-all handlers:

#include <iostream>
#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"

int main()
{
    try
    {
        rapidxml::file<> xmlFile("test.xml");
        rapidxml::xml_document<> doc;
        doc.parse<0>(xmlFile.data());
    }
    catch (const std::runtime_error& e)
    {
        std::cerr << "Runtime error was: " << e.what() << std::endl;
    }
    catch (const rapidxml::parse_error& e)
    {
        std::cerr << "Parse error was: " << e.what() << std::endl;
    }
    catch (const std::exception& e)
    {
        std::cerr << "Error was: " << e.what() << std::endl;
    }
    catch (...)
    {
        std::cerr << "An unknown error occurred." << std::endl;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I handle Import Errors?

How do I handle errors in a deferred function?

How do I handle errors with promises?

How do I handle JavaScript Fetch errors?

How do I handle transaction errors?

How Do I Handle Errors Globally in TestCafe

How do I handle stripe errors for "redirectToCheckout"?

How do i handle errors in Dio package

How do I handle errors in passport.deserializeUser()?

How do I handle errors in a worker pool using WaitGroup?

How do I handle errors in mapped functions in AWS Glue?

How do I handle errors that are already handled by another error?

How do I handle errors when responseType is blob using Vuejs?

In React, how do I handle errors coming back from the server?

How do i handle errors correctly and prevent showing folder directory

How do I handle errors in an f-string?

How do I handle different errors when working with an API?

How do I handle errors from cleanup / destroy functions

How do calculators handle floating point errors?

How can I handle errors in picocli?

How can I handle _popen() errors in C?

How should i handle dism powershell errors?

How can I handle input errors in HotChocolate?

How do I handle network errors so they don't appear in chrome developer console?

How do I handle errors without checked exceptions in a services orchestration scenario?

How do I capture the exit code / handle errors correctly when using process substitution?

(Discord PY) How do I handle slash command errors from different cog files?

How do I handle errors for fs.createReadStream() in Node.js?

How do I handle errors in a function called from a route handler in Express, NodeJS?