Forum Discussion

Matthew McDermott's avatar
Matthew McDermott
Brass Contributor
Dec 12, 2017

Proper Exception Handling when authoring PnP-PowerShell Cmdlets

I am wondering about the proper or recommended way to handle Exceptions in the PnP PowerShell cmdlets. I'll use the Get-PnPTerm cmdlet as an example. Currently, if the term does not exist the cmdlet throws a non terminating error: Get-PnPTerm : Specified argument was out of the range of valid values.

Personally, I don't see this as an exception, the term does not exist it's one of the expected conditions. (It also clutters up the output. :))

My question is, should the cmdlet return null or NotFoundException rather than the current ArgumentOutOfRangeException? Or, should we just leave it to the user to suppress the errors with -ErrorAction options?

  • If you use the cmdlets in your own PS script I think you can handle properly the exceptions produced when executing Gt-PnPTerm...the other option, since PnP is a community initiative, is to participate in the project and help on improving these cmdlets
    • Matthew McDermott's avatar
      Matthew McDermott
      Brass Contributor

      That is what I am asking, in general, if I write new cmdlets, what is the preferred handling approach? (That's why the title reads "when authoring".)

Resources