Powershell: try/catch/finally cannot catch non-terminating error
来源:互联网 发布:端口数据采集软件 编辑:程序博客网 时间:2024/05/17 12:03
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") try { $cmd = "Stop-Process -Name abc" Write-Warning "$cmd" Invoke-Expression $cmd Write-Host -Object "Killed Successfully!!!" -ForegroundColor Green } catch { $msg = $_.Exception.Message; [System.Windows.Forms.MessageBox]::Show($msg, "Error", [System.Windows.Forms.MessageBoxButtons]::OK); }
In case abc process does not exist, the catch block does not work because the error is non-terminating error, only terminating error can be caught in Powershell.
Get-Process : Cannot find a process with the name "abc". Verify the process name and call the cmdlet again.
At line:1 char:12
+ Get-Process <<<< abc
+ CategoryInfo : ObjectNotFound: (abc:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
we can resovle this issue by setting $ErrorActionPreference to Stop.
$ErrorActionPreference = "Stop"
or
add -ErrorAction Stop in cmdlet.
Stop-Process -Name abc -ErrorAction Stop
Similarly, Get-Content cmdlet also generate non-terminating error.
http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell
0 0
- Powershell: try/catch/finally cannot catch non-terminating error
- Error Handler/Exception Clause/Try ... Catch ... Finally
- try..catch..finally
- try-catch-finally
- Try-catch-finally机制
- 关于try-catch-finally
- C# try catch finally
- try...catch...finally
- try catch finally用法
- Try ---Catch----Finally
- try...catch... finally
- try....catch...finally 妙用
- try{} catch(){}finally{}执行
- Try Catch Finally
- try-catch-finally
- C# try, catch ,finally
- try catch finally
- try catch finally
- Win8.1蓝牙音频
- eclipse中注释模板个性化
- xcode6创建pch文件方式
- 统计学习方法笔记(一)——统计学习的基本概念
- 程序员找不到开发工作的6大原因
- Powershell: try/catch/finally cannot catch non-terminating error
- n个骰子 点数和 概率计算
- VC debug不能设断点
- 替换空格
- 解决evince打开pdf文件遇到的错误及解决方法
- C++多维数组的声明和使用
- inline_depth和inline_recursion编译指令
- oracle数据库不小心删除了表 解决办法。
- C++ 与 C 的规范不同之处