June 13, 2003
- updated February 8, 2006
In MovableType, blog pages are generated from templates. Sometimes re-building a page may fail for various reasons at which time MovableType reports an error. The tags in this plugin allow you to handle errors that occur during the building stage of your pages. The tags are modeled after the try-catch error handling approach common to languages such as Java and C++. You would enclose the part of template where you want to handle errors into MTExceptionTry tags. If an error occurs while building the page, the MTExceptionTry simply returns an empty string, instead of causing an error. You can optionally specify an MTExceptionCatch block inside your MTExceptionTry block. If an error occurs, the contents of the catch block are displayed.
Templates are actually processed in two stages. At the first stage, the template is compiled into sections of text and tags. During this stage MovableType reports any syntax errors that are in your template, for example if you have an <MTEntries> tag without a corresponding </MTEntries> tag. After a template has been compiled, MovableType uses it to build files from entries in the database. During this stage of errors may be reported. For example, you might be using a tag outside of it’s context—such as using an MTCategoryLabel tag when you are not in a category template. Or the tag may fail for other reason—such as trying to MTInclude a module that doesn’t exist or trying or using MTGetVar for a variable that has not been set. Since this plugin gets used during the building stage, it cannot catch any syntax errors that you run during the compile stage.
Example: The following code displays the contents of the variable “x”. If the variable “x” has not been defined, an empty string is displayed (instead of an error):
Example: The following code displays the contents of the module specified by the MTCategoryLabel. If the module doesn’t exist, the module “Category Default” is used instead:
<MTExceptionCatch><MTLoad module="Category Default"></MTExceptionCatch>