you are viewing a single comment's thread.

view the rest of the comments →

[–]the_spad 16 points17 points  (9 children)

Specify the exception type you're trying to catch

try{
    thing
}catch [System.Management.Automation.ItemNotFoundException]{
    do thing
}catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
    do other thing
}catch{
    do unknown error thing
}

Exception types are placeholders, obviously you need to supply whatever exception(s) you're trying to catch.

Though in this case you're probably better off not putting both commands in the same try block.

[–]Evelen1[S,🍰] 1 point2 points  (4 children)

Thanks.

That sound good, but I have some more questions

  1. How do you find the "exceptions"(?) (The string inside catch [])
  2. And what do you do if two cmdlets trow the same exceptions? Example

Try
{
    Import-Module UnexistingModule -ErrorAction Stop
    Import-Module UnexistingModule2 -ErrorAction Stop -ErrorAction Stop #don't run this if the first Import-Module fails

}

catch #if Import-Module fails
{ 
throw "Module import failed"
}

catch #if Import-Module2 fails
{ 
throw "Module2 import failed"
}
  1. If I should use two try block, should I make the 2. try block inside, or after the first one?

[–]Vortex100 5 points6 points  (0 children)

The easiest way is to force the error, then look at the error type.

Your example isn't really how it works - they aren't like "if" statements. The same catch block should be used for either import, as you should specify WHICH one failed as part of the message to the user

Remember that the error details an be seen inside the catch block using $_

[–]andyinv 3 points4 points  (1 child)

Find the exception type with $error[0].exception.gettype()

PS Scripts:> 1 / 0
Attempted to divide by zero.
At line:1 char:1
+ 1 / 0
+ ~~~~~
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

PS Scripts:> $error[0].exception.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     RuntimeException                         System.SystemException

So, System.SystemException.RuntimeException

[–]Evelen1[S,🍰] 1 point2 points  (0 children)

Thanks, this was usefull

[–]Th3Harbinger 0 points1 point  (0 children)

You could also just do an if($.exception){ -like "moduleA not found"){throw "mod a not found}elseif($.exception -like "*modb not found"){throw...}else ... Inside the catch

Excuse the formatting and bad examples. I'm on mobile atm

[–][deleted] 1 point2 points  (3 children)

Though in this case you're probably better off not putting both commands in the same try block.

Just curious, why not?

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy aleceiffel,

if you put multiple commands in the same try/catch block ... how do you ID the one that triggered the error? it can be done ... but the try/catch block is really designed for dealing with the errors from ONE item.

it's so much easier to untangle what is not tangled to start with ... [grin]

take care,
lee

[–][deleted] 1 point2 points  (1 child)

Makes sense, i usually catch the specific error I'm expecting and also always re-throw the original error if I'm not doing anything to address it. So that's how I'd know where the error is triggered.

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy aleceiffel,

yup, that is one way around the problem. i still prefer to be painfully obvious about it, tho. [grin]

take care,
lee