下面脚本中的第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:\Temp\1.cs" # line 2 } catch { # Discovering the full type name of an exception Write-Host $_.Exception.gettype().fullName Write-Host $_.Exception.message }