Throw Exceptions Contract
Purpose
This Code Provider adds the guard precondition to method parameter(s) or a variable assignment. The precondition ensures the passed or assigned values are not empty and throws the ArgumentException (or ArgumentNullException) in case they are.
Availability
Available in the following cases.
- When the caret is at the beginning of a method body. In this case, the contract will test all method parameters and strings.
- When the caret is on a method's parameter name. In this case, the contract will test only the selected parameter.
- When the caret is on a variable assignment.
Note
This Code Provider may be unavailable for variables of a non-nullable type, for instance, Integer.
Usage
Place the caret at the beginning of a method body.
Note
The blinking cursor shows the caret's position at which the Code Provider is available.
public bool AddRecord(string name, object data) {│
//...
return true;
}
Public Function AddRecord(ByVal name As String, ByVal data As Object) As Boolean│
'...
Return True
End Sub
- Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
- Select Add Contract | Throw Exceptions Contract from the menu.
After execution, the Code Provider adds the guard conditional against all the method parameters.
public bool AddRecord(string name, object data) {
if (string.IsNullOrEmpty(name))
throw new ArgumentException($"{nameof(name)} is null or empty.", nameof(name));
if (data == null)
throw new ArgumentNullException(nameof(data), $"{nameof(data)} is null.");
//...
return true;
}
Public Function AddRecord(ByVal name As String, ByVal data As Object) As Boolean
If String.IsNullOrEmpty(name) Then
Throw New ArgumentException($"{NameOf(name)} is nothing or empty.", NameOf(name))
End If
If data Is Nothing Then
Throw New ArgumentNullException(NameOf(data), $"{NameOf(data)} is nothing.")
End If
'...
Return True
End Function
See Also
We are updating the DevExpress product documentation website and this page is part of our new experience. During this transition period, product documentation remains available in our previous format at documentation.devexpress.com. Learn More...