Tutorial: Exception Handling in CookXml


 

 

Support This Project

 

stats counter

 

Logo SourceForge.net Logo IntelliJ IDEA

A feature introduced in CookXml since 2.0 is the ability to handle exceptions. This feature allows developers to intercept these exceptions and deal with them, by logging, suppressing, or generating an error. This tutorial basically teaches how it works inside CookXml.

Installing ExceptionHandler

ExceptionHandler can be set or get on a global basis by calling CookXml.setDefaultExceptionHandler(ExceptionHandler defaultExceptionHandler) and CookXml.getDefaultExceptionHandler (). This default handler is called when individual CookXml objects do not have their ExceptionHandler set.

Individual CookXml ExceptionHandler can be set/get by calling CookXml..setExceptionHandler(ExceptionHandler exceptionHandler) and CookXml.getEceptionHandler (), respectively.

Exceptions Intercepted

The following table shows the major exceptions intercepted in chronological ordering.

Exception Description
NoCreatorException This exception is thrown by DecodeEngine when it starts to process a tag and this tag is not found in the tag library.
CreatorException This exception is thrown by DecodeEngine when the creator for the tag is called and an exception is generated.
NoAdderException This exception is generated when DecodeEngine couldn't find an Adder in the tag library for the given parent and child.
AdderException This exception is thrown when an object is added to the parent object. Depending on how the creator for the object sets things up, Adder maybe called before or after the object is fully configured.
NoSetterException This exception is generated when DecodeEngine couldn't find a setter in the tag library for the given tag and attribute. In most CookXml setup though, this exception should never be generated.
SetterException This exception is thrown when a setter is called to set the attribute of the object created.
VarLookupException This exception is thrown when DecodeEngine fails to read/write a variable.

There are other types of exceptions as well.

Exception Description
NoDefaultConstructorException This exception is thrown by DefaultCreator at library setup phase when no default constructor is found for a given class.
ConverterException This exception is thrown whenever a converter fails to convert a string to a type that is usable by the target object. This exception is usually embeded within SetterException.

Finally, if an exception occurs in CookXml.xmlDecode function, it will not be wrapped around by CookXmlException.

What to Do with the Exception

Well, here is a list of things off the top of my head.

  • Ignore it. This is the case when you want CookXml to decode as much as possible.
  • Generate a CookXmlException. This is the case when you want CookXml to immediately return upon an error.
  • In deciding what to do with an exception, you can create logs, dump the exception to screen, etc.

(c) Copyright 2004-2007 Heng Yuan. All rights reserved.

Valid XHTML 1.0! Valid CSS!