To handle an exception, you need to add the Continue statement to your trap: Now, the trap works as expected. It does whatever you specified in the trap script block, and PowerShell does not get to see the exception anymore.You no longer get the red error message.
PowerShell Tutorial – Try Catch Finally and error handling in PowerShell. One of the key parts of any good PowerShell script is error handling. Even in the shortest script, being able to handle errors helps to ensure that an unexpected event will not go on to wreck the system you are working on. Take the example below. Basically, you tell PowerShell to treat it as terminating. To do this you use the ErrorAction parameter. Every PowerShell cmdlet supports ErrorAction. By specifying-ErrorAction Stop on the end of a cmdlet you ensure that any errors it throws are treated as terminating and can be caught. Funny enough, your trap is never called. Instead, PowerShell outputs its default error message. This is because Cmdlets handle their errors themselves, and no exception is raised. The error is invisible to your trap. To make it visible, you need to set $ErrorActionPreference to 'Stop'. Note that your Trap did not handle the exception because it did not call Continue. Instead, it just modified the existing error record and left it to PowerShell to output the error information. Of course, in real life you would first check error details and then create a meaningful replacement message. Wrong. In PowerShell, just because you've seen an error message doesn't mean an exception was created. You can't trap or handle an error message. You can only trap and handle exceptions. What you just saw was an example of a non-terminating exception. First, You Need an Error. To trap and handle an error, you actually need one to occur. Technically, in PowerShell terminology, you need an exception to occur. That can actually be a little tricky to do, believe it or not. For example, try running the following command If you set $ErrorActionPreference = 'Stop' globally then you will get Error testing :) printed, as expected. But your $ErrorActionPreference is presumably 'Continue' : in that case there is no terminating error/exception and you just get the non terminating error message printed to the host by the engine. Instead of the global $ErrorActionPreference option you can also play with Get-WmiObject parameter ErrorAction.  Powershell Exception Handling. Posted July 11, 2011 by Chris in Powershell. Tagged: exception, powershell. Leave a Comment. As they say, anything that can go wrong will go wrong. Users can try to access files they don’t have permissions to, servers can go down, disk space can disappear, etc. This is because your Trap did not really handle the exception. To handle an exception, you need to add the Continue statement to your trap: Now, the trap works as expected. It does whatever you specified in the trap script block, and PowerShell does not get to see the exception anymore. You no longer get the red error message. Note that your Trap did not handle the exception because it did not call Continue. Instead, it just modified the existing error record and left it to PowerShell to output the error information. Of course, in real life you would first check error details and then create a meaningful replacement message. One of the key parts of any good PowerShell script is error handling. Even in the shortest script, being able to handle errors helps to ensure that an unexpected event will not go on to wreck the system you are working on. Basically, you tell PowerShell to treat it as terminating. To do this you use the ErrorAction parameter. Every PowerShell cmdlet supports ErrorAction. By specifying-ErrorAction Stop on the end of a cmdlet you ensure that any errors it throws are treated as terminating and can be caught. To handle errors in your code, make sure you set the ErrorAction to Stop. Only then will cmdlets and native commands place errors in your control. To detect and respond to errors, use either a local try/catch-block (to catch errors in specific regions of your code) or trap (to catch all errors in the current scope). You can still not handle the error. When you place the code in a try/catch-block, the catch-block never executes: As you know from cmdlets, to handle errors you need to set the ErrorAction to Stop. With cmdlets, this was easy because each cmdlet has a-ErrorAction preference. Also note that whenever an error occurs in the try-block, PowerShell jumps to the corresponding catch-block and will not return and resume the try-block. This is why only Get-WMIObject is placed inside the try-block, not the Foreach-Object statement. You can still not handle the error. When you place the code in a try/catch-block, the catch-block never executes: As you know from cmdlets, to handle errors you need to set the ErrorAction to Stop. With cmdlets, this was easy because each cmdlet has a-ErrorAction preference.
http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell http://powershell.com/cs/blogs/tobias/archive/2008/09/29/trapping-errors.aspx http://windowsitpro.com/scripting/error-trapping-and-handling-powershell http://stackoverflow.com/questions/4691874/powershell-2-0-and-how-to-handle-exceptions https://wprogramming.wordpress.com/2011/07/11/powershell-exception-handling/ http://powershell.com/cs/blogs/tobias/archive/2008/09/29/trapping-errors.aspx http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell http://powershell.com/cs/blogs/ebookv2/archive/2012/03/18/chapter-11-error-handling.aspx http://powershell.com/cs/blogs/ebookv2/archive/2012/03/18/chapter-11-error-handling.aspx
how to handle exception in powershell