How to: Generate Errors and Warnings from Text Templates

You can generate custom error and warning messages from text templates and display them in the Error List window. You can use this technique to customize the behavior of your text templates for your language users.

To add error or warning messages as text template output

  1. In Solution Explorer, right-click the text template file that you want to edit, and then click Open.

    The file opens in the editor.

  2. Add the following code with the Error and Warning methods to the template:

    <#
        string name = null;
        if(string.IsNullOrEmpty(name))
        {
            //raise an error, this will appear in the error list
            Error("I have no name.");
        }
    
        name = "short";
        if(name.Length < 6)
        {
            //raise a warning, this will appear in the error list
            Warning("I have a really short name.");
        }
    #> 
    
    <#
        Dim name as string = ""
        If string.IsNullOrEmpty(name) Then
    
            'raise an error, this will appear in the error list
            Me.Error("I have no name.")
        End If
    
        name = "short"
        If name.Length < 6 Then
    
            'raise a warning, this will appear in the error list
            Me.Warning("I have a really short name.")
        End If
    #>
    

    Note

    The Error and Warning methods also have overload methods that take line and column numbers as additional arguments. For more information, see Error and Warning.

If the template throws any unhandled exceptions, they also appear in the Error List window and stop execution of the template.

To learn how to use errors and warnings in the context of creating and using a complete text template, see Walkthrough: Creating and Running Text Templates.

See Also

Reference

Error

Warning

Other Resources

Adding Code to Text Templates

Using Built-in Directives in Text Templates

Generating Artifacts By Using Text Templates

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.