问题描述
下面脚本中的第2行生成 -
Line 2 in the script below generates -
在行:1 char:8
+ [xml] $ x<<< = Get-内容4517.xml
+类别信息:MetadataError:(:) [],ArgumentTransformationMetadataException
+ FullyQualifiedErrorId:RuntimeException
At line:1 char:8 + [xml]$x <<<< = Get-Content 4517.xml + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException"
在第4行(脚本)中应该指定什么异常来捕捉上述错误?
What exception should be specified on line 4 (of the script) to catch the aforementioned error?
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
推荐答案
这是一种发现自己的完整类型名称的异常,这里的结果给出了由@Adrian Wright给出的System.Management.Automation.ArgumentTransformationMetadataException
Here is a way to discover yourself the full type name of an Exception, the result here gives System.Management.Automation.ArgumentTransformationMetadataException as given by @Adrian Wright.
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中应该使用什么异常类型来捕获由于无效字符引起的XML解析错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!