有没有一种方便的方法来捕获异常类型和内部异常以用于 try-catch 目的?
示例代码:
$a = 5
$b = Read-Host "Enter number"
$c = $a / $b #error if $b -eq 0
$d = get-content C:\I\Do\Not\Exist
Run Code Online (Sandbox Code Playgroud)
第 3 行将生成带有内部异常的运行时错误(编辑:修复了此命令 $Error[1].Exception.InnerException.GetType()),第 4 行将生成“标准”(?) 类型的异常 ($错误[0].Exception.GetType())。
是否可以使用同一行代码从这两者中获得所需的结果?
Ad1:第 3 行错误
At -path-:3 char:1
+ $c = $a / $b #error if $b -eq 0
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
Run Code Online (Sandbox Code Playgroud)
Ad2:第 4 行错误
get-content : Cannot find path 'C:\I\Do\Not\Exist' because it does not exist.
At …Run Code Online (Sandbox Code Playgroud)