SOLVED

Catching errors

%3CLINGO-SUB%20id%3D%22lingo-sub-265339%22%20slang%3D%22en-US%22%3ECatching%20errors%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-265339%22%20slang%3D%22en-US%22%3E%3CP%3ETrying%20to%20wrap%20my%20head%20around%20this.%20Let's%20say%20I%20execute%20the%20following%2C%20WITHOUT%20defining%20the%20variables%3A%3C%2FP%3E%0A%3CPRE%3Etry%20%7B%0A%20%20%20%20New-SmbShare%20-Description%20%22%24SmbShareDescription%22%20-FullAccess%20'everyone'%20-Name%20%22%24SmbShareName%22%20-Path%20%22%24SmbSharePath%22%20%7C%20Out-Null%0A%7D%0Acatch%20%7B%0A%20%20%20%20Write-Warning%20%24_%0A%7D%3C%2FPRE%3E%0A%3CP%3EThe%20code%20in%20the%20try%20block%20will%20fail%20with%20a%20ParameterBindingValidationException%2C%20and%20the%20catch%20block%20will%20execute%2C%20as%20expected.%3C%2FP%3E%0A%0A%3CP%3ENow%2C%20if%20I%20define%20the%20variables%20first%20and%20execute%2C%20the%20try%20code%20executes%20and%20succeeds%2C%20no%20need%20for%20the%20catch%20code%20to%20run.%20If%20I%20run%20it%20again%2C%20with%20the%20same%20values%2C%20the%20try%20code%20fails%20with%20a%20CimException%20(%22the%20name%20has%20already%20been%20shared%22)%2C%20as%20expected%2C%20but%20throws%20a%20visible%20exception%20error%2C%20and%20the%20catch%20block%20does%20NOT%20execute.%3C%2FP%3E%0A%0A%3CP%3EObviously%20I%20can%20set%20ErrorAction%20to%20SilentlyContinue%20to%20suppress%20the%20visible%20exception%20error%2C%20but%20I'm%20curious%20why%20the%20catch%20block%20doesn't%20execute%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-265339%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-265694%22%20slang%3D%22en-US%22%3ERe%3A%20Catching%20errors%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-265694%22%20slang%3D%22en-US%22%3EThanks.%20I've%20looked%20around%20to%20see%20how%20to%20tell%20if%20an%20error%20will%20be%20terminating%2C%20and%20not%20finding%20a%20reference.%20Anyone%20have%20info%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-265503%22%20slang%3D%22en-US%22%3ERe%3A%20Catching%20errors%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-265503%22%20slang%3D%22en-US%22%3E%3CP%3EYup%2C%20you%20need%20to%20throw%20a%20terminating%20error%2C%20which%20in%20this%20case%20can%20be%20done%20by%20adding%20the%20-ErrorAction%20Stop%20parameter.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%5B10%3A05%3A49%5D%23%20try%20%7B%20New-SmbShare%20test%20-Path%20%5C%20%7D%20catch%20%7B%22aaa%22%7D%0ANew-SmbShare%20%3A%20The%20filename%2C%20directory%20name%2C%20or%20volume%20label%20syntax%20is%20incorrect.%0AAt%20line%3A1%20char%3A7%0A%2B%20try%20%7B%20New-SmbShare%20test%20-Path%20%5C%20%7D%20catch%20%7B%22aaa%22%7D%0A%2B%20%20%20%20%20%20%20~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20NotSpecified%3A%20(MSFT_SMBShare%3AROOT%2FMicrosoft%2FWindows%2FSMB%2FMSFT_SMBShare)%20%5BNew-SmbShare%5D%2C%20CimException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20Windows%20System%20Error%20123%2CNew-SmbShare%0A%0A%5B10%3A06%3A00%5D%23%20try%20%7B%20New-SmbShare%20test%20-Path%20%5C%20-ErrorAction%20Stop%7D%20catch%20%7B%22aaa%22%7D%0Aaaa%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-265413%22%20slang%3D%22en-US%22%3ERe%3A%20Catching%20errors%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-265413%22%20slang%3D%22en-US%22%3E%3CP%3EIt%20only%20runs%20on%20a%20terminating%20error.%3CBR%20%2F%3EIs%20share%20already%20exists%20a%20terminating%20error%2C%20possibly%20not%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
MVP

Trying to wrap my head around this. Let's say I execute the following, WITHOUT defining the variables:

try {
    New-SmbShare -Description "$SmbShareDescription" -FullAccess 'everyone' -Name "$SmbShareName" -Path "$SmbSharePath" | Out-Null
}
catch {
    Write-Warning $_
}

The code in the try block will fail with a ParameterBindingValidationException, and the catch block will execute, as expected.

Now, if I define the variables first and execute, the try code executes and succeeds, no need for the catch code to run. If I run it again, with the same values, the try code fails with a CimException ("the name has already been shared"), as expected, but throws a visible exception error, and the catch block does NOT execute.

Obviously I can set ErrorAction to SilentlyContinue to suppress the visible exception error, but I'm curious why the catch block doesn't execute?

3 Replies
Highlighted

It only runs on a terminating error.
Is share already exists a terminating error, possibly not

Highlighted
Solution

Yup, you need to throw a terminating error, which in this case can be done by adding the -ErrorAction Stop parameter.

 

[10:05:49]# try { New-SmbShare test -Path \ } catch {"aaa"}
New-SmbShare : The filename, directory name, or volume label syntax is incorrect.
At line:1 char:7
+ try { New-SmbShare test -Path \ } catch {"aaa"}
+       ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [New-SmbShare], CimException
    + FullyQualifiedErrorId : Windows System Error 123,New-SmbShare

[10:06:00]# try { New-SmbShare test -Path \ -ErrorAction Stop} catch {"aaa"}
aaa
Highlighted
Thanks. I've looked around to see how to tell if an error will be terminating, and not finding a reference. Anyone have info?