How to deal with Exceptions and -ErrorAction SilentlyContinue

Hi all.

I have stumbled upon a problem with Exception handling and I am a bit lost.

I am in the process of fixing several Powershell modules to make Cmdlets throw exceptions instead of erroring to the console. Previously, all these Cmdlets reported errors by means of return codes (horror!).

So far so good. The real problem is that most existing modules are full of this kind of code:

# Check if user exists
$user = Get-AdUser $user -ErrorAction SilentlyContinue

if($user) {
	#do something with $user
}

Now, if I wrap this kind of code into a try...catch block I still get an exception!

[PS] C:\> try {
>> get-aduser dfjgdfjgj -erroraction silentlycontinue
>> } catch {
>> write-host "exception caught"
>> }
>>
exception caught
[PS] C:\>

Since this kind of check is all over the code, wrapping all of these in try...catch blocks would be a MASSIVE job. I am wondering if there is a way to make -ErrorAction SilentlyContinue not raise the exception at all.

Has anyone had this kind of issue before? Has anyone got any idea?

Thanks!

July 1st, 2013 7:44pm

Here is a chapter on Error Handling from powershell.com, maybe it will help you.

Error Handling

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2013 7:52pm

If you are looking to trap specific exceptions, you can specify the exception to catch like this:

catch [System.ArgumentException]
{
    ...
}


However, if you generically catch System.Exception, then it will catch the exception from Get-ADUser.

From your example, it's not entirely clear what you are trying to accomplish. Would using -ErrorVariable and capturing the variable help? If you could give a more specific example of what you're trying to achieve, it would be helpful. Are you trying to return an exception to a calling function without outputting it?

Mike

July 1st, 2013 9:17pm

Yeah, no matter what Try and Catch will only trap terminating errors.  Any non terminating errors will still display normally.  What you need to do is have your erroractionpreference set to stop.  That will turn normal errors into terminating errors thereby exiting the try and activating the catch.
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2013 10:06pm

ErrorAction only applies to non-terminating errors. Terminating errors always have to be handled either by the try/catch/finally statements, or by the trap statement.
July 2nd, 2013 3:02am

Check out the trap statement. It's somewhat like "On Error Goto" in VB, if you're familiar with that.
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 8:24am

Well, I am trying to catch any exception that might occur in the code.

Problem is, there's a lot of those "check if user/mailbox doesn't exist" in that code, and all of those, because of the way they are written, also raise exceptions.

I was just wondering if there is a clever way to implement these negative checks without them raising an exception and without having to wrap them into a try..catch block.

July 2nd, 2013 11:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics