I am looking to be able to handle certainly exception types differently, and optionally.
If I pass a type to be ignored as an argument $ignoreExceptionType
I can use
try {
... code that might generate an exception
} catch {
if (-not $ignoreExceptionType -or (-not ($PSItem.Exception -is $ignoreExceptionType)) {
... do actual exception handling
}
}
However, I wonder if there is a way to have a dedicated catch
block, something like
} catch $ignoreExceptionType {
The fact that this variable is optional, and the conditional works fine. But if there is a mechanism to do it as a dedicated block I suspect it could be useful at some point.
CodePudding user response:
No, the syntax for the catch
block is restricted to a comma-separated list of 0 or more type names - no variable expansion or other deferable logic can be employed, the list of exception type names has to be hardcoded in the source code.
You can read more about try/catch/finally blocks in the about_Try_Catch_Finally
help file