- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想抛出带有一些附加属性的自定义 System.Management.Automation.ErrorRecord
:ContextMessage
和 SqlCmdHeader
。
我尝试创建继承自 System.Management.Automation.ErrorRecord
的新类:
class SqlRequestException : System.Management.Automation.ErrorRecord
{
[hashtable]$SqlCmdHeader
[string]$ContextMessage
SqlRequestException() { }
SqlRequestException(
[string]$message,
[hashtable]$sqlCmdHeader,
[System.Management.Automation.ErrorRecord]$errorRecord
) : base($errorRecord, $null)
{
$this.SqlCmdHeader = $sqlCmdHeader
$this.ContextMessage = $message
}
}
但是当我通过这段代码抛出错误时:
try
{
#exceptions is risen here
}
catch
{
throw ([SqlRequestException]::New(
"Some context message of exception which will help to understand the error without additional debugging of code",
$sqlHeader,
$_))
}
我只是进入了调用者 System.Management.Automation.ErrorRecord
而没有我很棒的属性。看起来 throw
将我的对象转换为 System.Management.Automation.ErrorRecord
并丢失了属性。
我想请你们帮我在我的脚本中实现这个逻辑。任何帮助将不胜感激!
我无法更改来电者的密码。它总是解析错误并将其写入日志:
try
{
# calling the method from module where I try to throw custom ErrorRecord
}
catch
{
Write-Log -Text $_
}
我不能使用 System.Exception
因为当我抛出创建的异常时,所有原始信息(例如 InvocationInfo
、ScriptStackTrace
等)都来自ErrorRecord
丢失。我找不到 throw
语句的源代码来理解它到底是如何工作的(也许有人可以指点我?)
我看到有 TargetObject
属性可用于存储一些附加信息,但我看不到在 throw
语句之前将某些内容放入该属性的方法,因为该属性为 ReadOnly
,我认为该属性并非旨在存储其他用户的数据。
最佳答案
虽然 System.Management.Automation.ErrorRecord
类在技术上不是密封的,文档也没有提到子类化,看起来子类化 ErrorRecord
实际上不支持:
如您所见,您抛出
的原始SqlRequestException
实例并没有像这样进入调用堆栈,大概 因为涉及复制到基类的实例。
改为子类 System.Exception
并让 PowerShell 自动将您的异常包装在 [ErrorRecord]
实例中,该实例的 .Exception
稍后可以查询的属性:
# Make your custom exception derive from System.Exception.
class SqlRequestException : System.Exception
{
[hashtable]$SqlCmdHeader
[string]$ContextMessage
# Construct the exception with the one passed as an
# argument as the *inner* exception.
SqlRequestException(
[string]$contextMessage,
[hashtable]$sqlCmdHeader,
[exception]$innerException
) : base($innerException.Message, $innerException)
{
$this.ContextMessage = $contextMessage
$this.SqlCmdHeader = $sqlCmdHeader
}
}
try { # Outer `try` to simulate a caller.
try # Your code.
{
[int]::Parse('not a number') # Sample exception.
}
catch
{
# Throw your custom exception that wraps the original exception
# ($_.Exception) via its .InnerException property.
# PowerShell itself then automatically wraps your custom exception in
# an [ErrorRecord] instance.
throw ([SqlRequestException]::New(
'custom message',
@{ header1 = 'foo' },
$_.Exception))
}
} catch { # The (simulated) caller's catch block.
# Now you can access your custom properties via $_.Exception.
@"
Error: $_
Custom properties:
ContextMessage: $($_.Exception.ContextMessage)
SqlCmdHeader: $($_.Exception.SqlCmdHeader | Out-String)
"@
}
上面的结果如下,表明两个自定义属性都被传递了:
Error: Exception calling "Parse" with "1" argument(s): "Input string was not in a correct format."
Custom properties:
ContextMessage: custom message
SqlCmdHeader:
Name Value
---- -----
header1 foo
自定义异常的消息是原始异常的消息;要获取原始异常的详细信息,请访问 $_.InnerException
。
关于powershell - 有没有办法通过自定义 System.Management.Automation.ErrorRecord 抛出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60543128/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
在现代 IDE 中,有一个键盘快捷键可以通过键入文件名称来打开文件,而无需将手放在鼠标上。例如: Eclipse:Cmd|Ctrl + Shift + R -> 打开资源 IntelliJ:Cmd|C
有什么东西会等待事件发生(我正在等待的是 WebBrowser.DocumentCompleted),然后执行代码吗?像这样: If (WebBrowser.DocumentCompleted) 不会
我使用 PHP Minify,它很棒。但我的问题是,是否有任何 PHP 插件或其他东西可以自动检测 javascript/css 代码并自动缩小它?谢谢。 最佳答案 Javascript 压缩器? 看
有没有一种语言,类似什么CoffeeScript是JavaScript,编译成windows batch|cmd|command line的语言? 我指的cmd版本是基于NT的,尤其是XP sp3及以
我知道我可以 ,但是,我真的宁愿有一个任务,我可以从任何可以使用所有(或至少大部分)属性的操作系统调用 copy ,但这并没有消除 unix 上的权限。 我想知道是否已经有解决方案,或者我必须自己编
我正在使用 Vuejs(不使用 jQuery)开发一个项目,该项目需要像 jvectormap 这样的 map 但正如我所说,我没有使用 jQuery,那么是否有任何其他库可以在不使用 jQuery
想要进行一个简单的民意调查,甚至不需要基于 cookie,我不在乎投了多少票。有没有类似的插件或者简单的东西? 最佳答案 这是一个有用的教程 - 让我知道它是否适合您 using jQuery to
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
var FileBuff: TBytes; Pattern: TBytes; begin FileBuff := filetobytes(filename); Result := Co
我想要一个 vqmod xml 文件来添加一次上传多个图像的功能。身边有这样的事吗? 编辑:Opencart版本:2.1.0.1 最佳答案 最后我写了一个xml来添加到opencart 2.1.0.1
所以考虑这样的函数: public void setTemperature(double newTemperatureValue, TemperatureUnit unit) 其中Temperatur
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我是 ggplot2 的新手,一直在尝试找到一个全面的美学列表。我想我理解它们的目的,但很难知道哪些可以在各种情况下使用(主要是几何图形?)。 Hadley 的网站偶尔会在各个几何图形的页面上列出可用
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
是否有任何 PHP 函数可以将整数转换为十万和千万? 900800 -> 9,00,800 500800 -> 5,00,800 最佳答案 由于您已在问题标签中添加了 Yii,因此您可以按照 Yii
使用 Clojure 一段时间后,我积累了一些关于它的惰性的知识。我知道诸如map之类的常用API是否是惰性的。然而,当我开始使用一个不熟悉的API(例如with-open)时,我仍然感到怀疑。 是否
我的项目需要一个像 AvalonDock 这样的对接系统,但它的最后一次更新似乎是在 2013 年 6 月。是否有更多...积极开发的东西可以代替它? 最佳答案 AvalonDock 实际上相当成熟并
我正在寻找一个可以逆转 clojure 打嗝的函数 所以 turns into [:html] 等等 根据@kotarak的回答,这现在对我有用: (use 'net.cgrand.enliv
我是一名优秀的程序员,十分优秀!