Powershell如何獲取異常的名稱-創(chuàng)新互聯(lián)

Powershell里面一般處理異常分為中斷類型和不可中斷類型。前者一般是通過try..catch..finally處理,后者一般通過ErrorAction, ErrorVariable處理。

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)蕪湖縣免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了超過千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

通過try..catch處理的時候有一個問題就是catch后面跟的異常,他的名字到底怎么獲取的?

比如說我執(zhí)行,他會報錯,因為 nnnnn這個命令不存在。

PS C:\> a=nnnnn
a=nnnnn : The term 'a=nnnnn' 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
+ a=nnnnn
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (a=nnnnn:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如果我想使用try ..catch捕獲這個異常,如何知道這個異常的具體名字是什么?

可以通過$error這個變量來獲取,最新的報錯就是 $error[0], 通過他可以知道具體的Exception是什么,這樣就可以有的放矢了。

PS C:\> $Error[0] | fl * -f
PSMessageDetails      : 
Exception             : System.Management.Automation.CommandNotFoundException: The term 'a=nnnnn' 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 System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, 
                        ExecutionContext context)
                           at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
                           at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
                           at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, 
                        ExecutionContext context)
                           at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] 
                        commandRedirections, FunctionContext funcContext)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : a=nnnnn
CategoryInfo          : ObjectNotFound: (a=nnnnn:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

比如這樣就行了

PS C:\> 
try{
a=nnnnn
}catch [System.Management.Automation.CommandNotFoundException]{
"error1"
}
error1

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前名稱:Powershell如何獲取異常的名稱-創(chuàng)新互聯(lián)
當前路徑:http://bm7419.com/article14/dihcge.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站網(wǎng)站建設、網(wǎng)站收錄手機網(wǎng)站建設、企業(yè)網(wǎng)站制作定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設