加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Powershell中应该使用什么异常类型来捕获由于无效字符而导致的

发布时间:2020-12-16 23:49:01 所属栏目:百科 来源:网络整理
导读:下面脚本中的第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.xm
下面脚本中的第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
}

感谢您寻找(并回答)

阿德里安

这是一种发现自己的Exception的完整类型名称的方法,这里的结果给出了@Adrian Wright给出的System.Management.Automation.ArgumentTransformationMetadataException.
Clear-Host
try {
    [xml]$xml = Get-Content "c:Temp1.cs" # line 2
}
catch {
    # Discovering the full type name of an exception
    Write-Host $_.Exception.gettype().fullName
    Write-Host $_.Exception.message
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读