PowerShell 中的終止錯誤和非終止錯誤是什麼?
Powershell 在執行指令碼或命令時會生成兩種型別的錯誤:**終止錯誤**和**非終止錯誤**。
**終止錯誤** - 此錯誤由您建立的指令碼、函式或命令生成,它會停止或中斷指令碼的執行,從而無法執行下一行中的命令。要處理此錯誤,需要合適的機制,否則會顯示錯誤訊息。
例如:
PS C:\WINDOWS\system32>> This-commandnotexist This-commandnotexist : The term 'This-commandnotexist' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + This-commandnotexist + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (This-commandnotexist:String) [], CommandNotFo undException + FullyQualifiedErrorId : CommandNotFoundException
**非終止錯誤** - 此錯誤通常由內部 cmdlet 生成,並由它們自動處理,但錯誤不會終止管道的執行。您無法自動處理此類錯誤,因為預設情況下 **$ErrorActionPreference** 值為 **Continue**,但可以透過將它們轉換為終止錯誤來處理非終止錯誤。
在下面的示例中,我們將搜尋不存在的計算機上的邏輯磁碟。
PS C:\WINDOWS\system32>> Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist Get-WmiObject : The RPC server is unavailable. At line:1 char:1 + Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiO bjectCommand
上述錯誤由 cmdlet 生成,它是一個非終止錯誤。您可以使用 **ErrorAction** cmdlet、**$ErrorActionPreference** 變數和 **try、catch 和 finally** 塊來處理終止錯誤和非終止錯誤(透過將其轉換為終止錯誤)。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP