PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Add a rule to alert `catch [RuntimeException] { ... }`

Open daxian-dbw opened this issue 4 years ago • 2 comments

Summary of the new feature

In practice, catch [System.Management.Automation.RuntimeException] { ... } behaves the same as an unqualified catch block. That is, it catches any exception. See details in https://github.com/PowerShell/PowerShell/issues/16392. Reason for that is explained in https://github.com/PowerShell/PowerShell/issues/16392#issuecomment-966712592.

Proposed technical implementation details (optional)

It would be nice to have a rule to caution against using RuntimeException as the type for a catch clause.

What is the latest version of PSScriptAnalyzer at the point of writing

1.20.0

daxian-dbw avatar Nov 12 '21 00:11 daxian-dbw

Interesting. I did a search on GitHub and it seems there is no public usage of that, therefore I am not sure if just documenting this edge case is good enough. Marking as up for grabs. https://github.com/search?q=catch+%5BRuntimeException%5D&type=code

bergmeister avatar Nov 13 '21 18:11 bergmeister

@bergmeister The old GitHub code search isn't very good. When I tried their new "dedicated" code search I see at least some of public usage: https://cs.github.com/?scopeName=All+repos&scope=&q=catch%20%5BSystem.Management.Automation.RuntimeException%5D

andyleejordan avatar Nov 17 '21 17:11 andyleejordan