Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual Studio SDK
 How to: Generate Errors and Warning...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Domain-Specific Language Tools 
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:

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

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

    NoteNote

    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.

For an illustration of using errors and warnings within the context of creating and using a complete text template, see Walkthrough: Creating and Running Text Templates.

NoteNote

To debug text templates, you must set the debug parameter of the template directive. For more information, see How to: Debug Text Templates.

Security

For more information, see Security of Text Templates.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker