kimo pryvt
kimo pryvt

Reputation: 503

Powershell select different exceptions

I am creating a script that needs to handle all the exceptions available, when creating quotas for a file server.

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

In this script I have 2 exceptions that are identified with the same net code System.Management.Automation.MethodInvocationException: I need to be able to select between the two exceptions en the code and do different remediation task for each exception.

My question is who can I select the different exceptions to make the appropriate error handling of this two different exceptions with the same .net code.

Full exceptions

Exception : System.Management.Automation.MethodInvocationException: Exception calling "CreateQuota" with "1" argument(s): "Exception from HRESULT: 0x80045306" ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x80045306)
Exception from HRESULT: 0x80045306
--- End of inner exception stack trace ---
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)
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
--- End of inner exception stack trace ---
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
at System.Management.Automation.ComAdapter.MethodInvoke(PSMethod method, Object[] arguments)
at System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod method, Object[] arguments)
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)

Exception: System.Management.Automation.MethodInvocationException: Exception calling "Commit" with "0" argument(s): "Exception from HRESULT: 0x80045307"

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Runtime.InteropServices.COMException (0x80045307): Exception from HRESULT: 0x80045307
--- End of inner exception stack trace ---
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)
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
--- End of inner exception stack trace ---
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
at System.Management.Automation.ComAdapter.MethodInvoke(PSMethod method, Object[] arguments)
at System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod method, Object[] arguments)
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)

Upvotes: 0

Views: 2707

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

catch blocks can distinguish exceptions by class:

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

They can't distinguish between different exceptions of the same class. You need to differentiate yourself inside the catch block, for instance by HRESULT:

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

If the exception is nested in another exception you need to unroll it first:

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

See here for more information about try..catch.

Upvotes: 3

majkinetor
majkinetor

Reputation: 9036

You can try something like this:

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

Upvotes: -1

Related Questions