Powershell选择不同的例外

时间:2016-04-15 04:16:52

标签: powershell exception error-handling

在为文件服务器创建配额时,我正在创建一个需要处理所有可用异常的脚本。

try
{
    ### Create cuota ###
}
catch [System.Management.Automation.MethodInvocationException]
{
    Write-Host "Exception 0x80045306"
}
catch [System.Management.Automation.MethodInvocationException]
{
    Write-Host " Exception 0x80045307 "
}

在此脚本中,我有两个使用相同网络代码System.Management.Automation.MethodInvocationException标识的异常:我需要能够在代码中的两个异常之间进行选择,并为每个异常执行不同的补救任务。

我的问题是,我可以选择不同的异常,以便使用相同的.net代码对这两个不同的异常进行适当的错误处理。

完全例外

  

异常:System.Management.Automation.MethodInvocationException:异常调用" CreateQuota"用" 1"参数:" HRESULT的异常:0x80045306" ---> System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---> System.Runtime.InteropServices.COMException(0x80045306)
  来自HRESULT的例外:0x80045306
  ---内部异常堆栈跟踪结束---
  at System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,Int32 culture,String [] namedParameters)
  at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)
  在System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,Object []参数)
  ---内部异常堆栈跟踪结束---
  在System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,Object []参数)
  在System.Management.Automation.ComAdapter.MethodInvoke(PSMethod方法,Object []参数)
  在System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod方法,Object []参数)
  at System.Management.Automation.ParserOps.CallMethod(Token token,Object target,String methodName,Object [] paramArray,Boolean callStatic,Object valueToSet)
  at System.Management.Automation.MethodCallNode.InvokeMethod(Object target,Object [] arguments,Object value)
  at System.Management.Automation.MethodCallNode.Execute(Array input,Pipe outputPipe,ExecutionContext context)
  at System.Management.Automation.AssignmentStatementNode.Execute(Array input,Pipe outputPipe,ExecutionContext context)
  at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement,Array input,Pipe outputPipe,ArrayList& resultList,ExecutionContext context)

     

异常:System.Management.Automation.MethodInvocationException:异常调用"提交"用" 0"参数:" HRESULT的异常:0x80045307"

     

System.Reflection.TargetInvocationException:调用目标抛出了异常。
  System.Runtime.InteropServices.COMException(0x80045307):来自HRESULT的异常:0x80045307
  ---内部异常堆栈跟踪结束---
  at System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,Int32 culture,String [] namedParameters)
  at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)
  在System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,Object []参数)
  ---内部异常堆栈跟踪结束---
  在System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,Object []参数)
  在System.Management.Automation.ComAdapter.MethodInvoke(PSMethod方法,Object []参数)
  在System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod方法,Object []参数)
  at System.Management.Automation.ParserOps.CallMethod(Token token,Object target,String methodName,Object [] paramArray,Boolean callStatic,Object valueToSet)
  at System.Management.Automation.MethodCallNode.InvokeMethod(Object target,Object [] arguments,Object value)
  at System.Management.Automation.MethodCallNode.Execute(Array input,Pipe outputPipe,ExecutionContext context)
  at System.Management.Automation.ParseTreeNode.Execute(Array input,Pipe outputPipe,ArrayList& resultList,ExecutionContext context)
  at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement,Array input,Pipe outputPipe,ArrayList& resultList,ExecutionContext context)

2 个答案:

答案 0 :(得分:3)

catch块可以按类别区分异常:

try {
    ...
} catch [System.Net.WebException], [System.IO.IOException] {
    # handle WebException and IOException
} catch [System.Management.Automation.MethodInvocationException] {
    # handle MethodInvocationException
} catch {
    # handle all other exceptions
}

他们无法区分同一类的不同异常。您需要在catch块内区分自己,例如HRESULT

try {
    ...
} catch [System.Management.Automation.MethodInvocationException] {
    switch ($_.Exception.HResult) {
        0x80045306 { ... }
        0x80045307 { ... }
        default    { "Unknown HResult: $_" }
    }
}

如果异常嵌套在另一个异常中,则需要先将其展开:

try {
    ...
} catch [OuterException] {
    $e = $_.Exception.InnerException
    switch ($e.HResult) {
        ...
    }
}

有关try..catch

的详情,请参阅here

答案 1 :(得分:0)

您可以尝试这样的事情:

try {
   1/0
} 
catch{ 
   $et = $_.Exception.gettype().Name
   switch (et) {
      'RuntimeException' : { 'runtime' }
   }
}