Powershell: Creating and Throwing a New Exception

How can I create and throw a new exception in PowerShell?

I want to do different things for a specific error.

+76
powershell error-handling
Aug 16 2018-12-12T00:
source share
1 answer

To raise a specific exception, such as FileNotFoundException, use this format

if (-not (Test-Path $file)) { throw [System.IO.FileNotFoundException] "$file not found." } 

To create a generic exception, use the throw command followed by a line.

 throw "Error trying to do a task" 

When used inside a catch, you can provide additional information about what caused the error

+118
Jul 04 '14 at 23:05
source share



All Articles