Catch RegEx error

I have a TextField for RegEx search pattern input. When starting to type ‘[’ it creates an error because of a pattern language error: An exception of class RegExSearchPatternException was not handled.

I can’t catch these errors for some reason. This is the code, function input and output is ‘s’:

Var re As New RegEx

re.SearchPattern = TextFieldRegExPattern.Text
re.ReplacementPattern = TextFieldRegExReplace.Text

re.Options.ReplaceAllMatches = True

  s = re.Replace(s)
Catch err As  RegExSearchPatternException
end Try

Return s

Where do you place that code ?

Does your app crash? If not, what happens.

Sorry, still a bit new to the Try thing, it looks the Try code only works in the Build app, not the Debugger, which makes sense. Problem solved!

Catching two errors:

  newText = re.Replace(s)
Catch err As  OutOfBoundsException
Catch err As  RegExSearchPatternException
end Try

Try works in both. But in the debugger, it will stop where the Exception happens, and will continue into the Catch block only after you Resume or Step.

1 Like

When the debugger stops in cases like this where it is more of a nuisance to debugging than an aid, I add this above the try block:

#Pragma BreakOnExceptions False
try  ....
end try
#Pragma BreakOnExceptions Default

This will tell the debugger do NOT break when it hits an exception during that time, and the return to the default setting to allow other exceptions to still break. You could also use “true” here, but my preference is to use Default so it honors my current setting in the IDE.

The #Pragma is only in effect for the rest of that method, so you can leave off the restore to default if you are ok without breaking for the remainder of that method call.

1 Like

Thanks for the info!

Thanks for the tip!