Share via

OnError Element (MSBuild)


This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Causes one or more targets to execute, if the ContinueOnError attribute is false for a failed task.



<OnError ExecuteTargets="TargetName"  
    Condition="'String A'=='String B'" />  

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.


Attribute Description
Condition Optional attribute.

Condition to be evaluated. For more information, see Conditions.
ExecuteTargets Required attribute.

The targets to execute if a task fails. Separate multiple targets with semicolons. Multiple targets are executed in the order specified.

Child Elements


Parent Elements

Element Description
Target Container element for MSBuild tasks.


MSBuild executes the OnError element if one of the Target element's tasks fails with the ContinueOnError attribute set to ErrorAndStop (or false). When the task fails, the targets specified in the ExecuteTargets attribute is executed. If there is more than one OnError element in the target, the OnError elements are executed sequentially when the task fails.

For information about the ContinueOnError attribute, see Task Element (MSBuild). For information about targets, see Targets.


The following code executes the TaskOne and TaskTwo tasks. If TaskOne fails, MSBuild evaluates the OnError element and executes the OtherTarget target.

<Target Name="ThisTarget">  
    <TaskOne ContinueOnError="ErrorAndStop">  
    <OnError ExecuteTargets="OtherTarget" />  

See Also

Project File Schema Reference