Creating and throwing new exception

后端 未结 1 1237
小鲜肉
小鲜肉 2021-02-01 11:16

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

I want to do different things for a specific error.

相关标签:
1条回答
  • 2021-02-01 12:11

    To call a specific exception such as FileNotFoundException use this format

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

    To throw a general exception use the throw command followed by a string.

    throw "Error trying to do a task"
    

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

    0 讨论(0)
提交回复
热议问题