How can I catch errors returned from XML::Parser without exiting the code?
Normally, the XML::Parser module will immediately terminate when it finds mal-formed XML. This is, in fact, the way XML parsers should behave. There are cases however, where you may want to handle the error without exiting the program. In these cases, you can enclose the code that calls the parse() or parsefile() methods in an eval block like: eval { $p->parse($xml) }; or like: eval { $p->parsefile($filename) }; If an error occurs, it puts the error message into the $@ variable. Below is a short script that parses an XML file. It encloses the parsefile() method in an eval block and then prints the error message if an error occured.
Related Questions
- Does the code in finally block get executed if there is an exception and a return statement in a catch block?
- I get compiler errors when trying to compile the provided code without changing anything. What is wrong?
- What does RootStream Detect 2008 do to analyze my accounting data and catch errors and possible fraud?