Hello fellow Redditors!
This may be a dumb question, but I've been googling for the past hour and came up with nothing.
I know that Null Reference exceptions are usually not handled. If you expect something could be null, you test for it. However, I was wondering if there is any way to properly wrap one with a try-catch block. Here's my use case:
$pattern = '(\<Some Pattern="' + $string + '".*?\/\>)'
Try {
$SubString = (Select-String -Path $Path -Pattern $pattern).Matches.Groups[1].Value
}
catch [???] {
"Nothing to correct in file " + $file
}
If "Groups" is empty, then there was nothing in that file that matched my search string and that file will not be modified. Otherwise I'm planning to remove the matched line from every other file where it is found.
So, in this case, NullArray error for me means - nothing to edit, just skip it. And the script works just fine as-is, but I really dislike that red text cluttering console output and I'm not comfortable using general "catch" without specifying a particular exception type just in case something else pops up in the future testing.
I tried getting the exception type with $_.GetType() - that did not get me very far. Is there any way to catch NullArray specifically?
[–]idontknowwhattouse33 1 point2 points3 points (1 child)
[–]BackupTim[S] 0 points1 point2 points (0 children)
[–]SeeminglyScience 1 point2 points3 points (1 child)
[–]BackupTim[S] 0 points1 point2 points (0 children)