The generated wrapper classes throw a number of exceptions, all of which are derived from LtXmlLib16::CLtException. The CLtException exception class is capable of holding an inner exception describing the detail of where the error occured, to get the most from an exception it is best to view the whole chain of exceptions (the method CLtException.GetFullMessage() gets the error messages from all the nested exceptions for you).

A key point to note when catching exceptions is that all CLtException derived exceptions are thrown by value and caught by reference.

Example

Schema Fragment

<xs:element name="ElmSequence">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="BasicElement"/>
            <xs:element name="ElmChoiceComplexType" type="BasicComplexType"/>
            <xs:element name="stringElm" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
</xs:element>


Note the first element in ElmSequence must be BasicElement

Sample File Invalid_ElmSequence_BadOrder.xml

<?xml version="1.0" encoding="UTF-8"?>
<ElmSequence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ElmSequence.xsd">
    <ElmChoiceComplexType>
        <StringElm>ElmChoiceComplexTypestringElmValue</StringElm>
    </ElmChoiceComplexType>
    <BasicElement>
        <StringElm>BasicElementstringElmValue</StringElm>
    </BasicElement>
    <stringElm>stringElmValue</stringElm>
</ElmSequence>

Note the first child element of ElmSequence is NOT BasicElement, but ElmChoiceComplexType, making the XML document invalid.

Code

try
{
    ElmSequence::CElmSequencePtr spElm = ElmSequence::CElmSequence::CreateInstance();

    spElm->FromXmlFile(_T("Invalid_ElmSequence_BadOrder.xml"));
}
catch (CLtException& e)
{
    _tprintf(_T("Error - %s\n"), e.GetFullMessage().c_str());
}


Because the XML file is invalid an error is raised when we attempt to load it. Note the exception is caught by reference.

Output

Error - Failed to process the child elements in element [ElmSequence]
Failed to find a required element [BasicElement]

The first line is the error text from the exception we caught, the second line is the error text from the inner exception.
It is also posible for the innerexception to have an inner exception etc.

 

© 2001-2010 Liquid Technologies Limited. All rights reserved. Privacy Statement