# LastError
{: .no_toc }

Returns the message of the error that compiling the current pattern raised.

Syntax: *object*.**LastError**

*object*
: *required* An object expression that evaluates to a **RegExp**.

Returns a **String**: the description of the compile error, or an empty string when the pattern is valid.

## Remarks

The property compiles the pattern if it has not been compiled since [**Pattern**](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) or [**IgnoreCase**](/tB/Packages/Contributed/tbRegExp/RegExp/IgnoreCase.md) last changed. The compilation traps its error, so **LastError** raises nothing and leaves the **Err** object clear. The text is a sentence that names the fault, such as *Unterminated character class: a '[' without a matching ']'.* for the pattern `[abc`. [**LastErrorNumber**](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) is the number of the error and lists the faults.

[**Execute**](/tB/Packages/Contributed/tbRegExp/RegExp/Execute.md), [**Test**](/tB/Packages/Contributed/tbRegExp/RegExp/Test.md), [**Replace**](/tB/Packages/Contributed/tbRegExp/RegExp/Replace.md) and [**Split**](/tB/Packages/Contributed/tbRegExp/RegExp/Split.md) raise an error with this description when the pattern is invalid.

## Example

```tb check_build check_run
Dim re As RegExp
Set re = New RegExp
re.Pattern = "(\d+)"
Debug.Print Len(re.LastError)  ' 0

re.Pattern = "(\d+"
Debug.Print re.IsValid         ' False
Debug.Print Len(re.LastError) > 0  ' True

re.Pattern = "[abc"
Debug.Print re.LastError           ' Unterminated character class: a '[' without a matching ']'.

On Error Resume Next
re.Test "123"
Debug.Print Err.Description = re.LastError  ' True
```

## See Also

- [LastErrorNumber](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) property -- the number of the compile error
- [IsValid](/tB/Packages/Contributed/tbRegExp/RegExp/IsValid.md) property -- whether the pattern compiles
- [Pattern](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) property -- the regular expression
- [RegExp](/tB/Packages/Contributed/tbRegExp/RegExp/index.md) class -- overview
