gpt4 book ai didi

powershell - 在 Powershell 中应该使用什么异常类型来捕获由于无效字符引起的 XML 解析错误?

转载 作者:行者123 更新时间:2023-12-04 14:32:26 26 4
gpt4 key购买 nike

下面脚本中的第 2 行生成 -

"Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'→', hexadecimal value 0x1A, is an invalid character. Line 39, position 23."

At line:1 char:8 + [xml]$x <<<< = Get-Content 4517.xml + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException"



应该在(脚本的)第 4 行指定什么异常来捕获上述错误?
try {
[xml]$xml = Get-Content $file # line 2
}
catch [?] { # line 4
echo "XML parse error!"
# handle the parse error differently
}
catch {
echo $error
# some general error
}

感谢您的查找(和回答)

阿德里安

最佳答案

这是一种发现自己异常的完整类型名称的方法,这里的结果给出了@Adrian Wright 给出的 System.Management.Automation.ArgumentTransformationMetadataException。

Clear-Host
try {
[xml]$xml = Get-Content "c:\Temp\1.cs" # line 2
}
catch {
# Discovering the full type name of an exception
Write-Host $_.Exception.gettype().fullName
Write-Host $_.Exception.message
}

关于powershell - 在 Powershell 中应该使用什么异常类型来捕获由于无效字符引起的 XML 解析错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10392807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com