<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Exception Syntax in Microsoft Virtualization Discussions</title>
    <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/Exception-Syntax/m-p/8741#M425</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nope, you're not doing anything silly.&amp;nbsp; That's exactly how I would expect it to work too.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PowerShell is doing something silly though.&amp;nbsp; First, EVOLUMEDOESNOTEXIST is a non-terminating error for this cmdlet; you have to set the ErrorAction parameter or $ErrorActionPreference variable otherwise it will just write the error and continue.&amp;nbsp; When you change the ErrorAction to stop, PowerShell wraps the exception in an ErrorRecord object before throwing it down the stack.&amp;nbsp; Obviously "ErrorRecord" won't match the catch type.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are a number of ways to handle this appropriately, but matching the Exception property looks nice to me:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code"&gt;&lt;P&gt;$ErrorActionPreference = "Stop"&lt;/P&gt;&lt;P&gt;$volName = "CWSQLSnapInfX"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;try&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #$res = new-object NetApp.Ontapi.ApiResult&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #throw new-object NetApp.Ontapi.Filer.ErrnoException+EVOLUMEDOESNOTEXIST $res&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $hVol = get-naVol $volName&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "hVol: " + $hVol.Name&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;catch {&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($_.Exception.GetType() -match "EVOLUMEDOESNOTEXIST")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host -BackGroundColor Yellow " Netapp UnTrapped Exception...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Quitting...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host -BackGroundColor Green "UnTrapped Exception...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Type: " $_.GetType().FullName&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Exception: " $_.Exception&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;which outputs this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code"&gt;&lt;P&gt; Netapp UnTrapped Exception....&lt;/P&gt;&lt;P&gt;Quitting....&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Type:&amp;nbsp; System.Management.Automation.ErrorRecord&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Exception:&amp;nbsp; NetApp.Ontapi.ErrnoException+EVOLUMEDOESNOTEXIST: No volume named 'CWSQLSnapInfX' exists&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; at NetApp.Ontapi.NaApi`1.Invoke(INaServer server)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; at DataONTAP.PowerShell.SDK.Cmdlets.Volume.GetNaVol.ProcessRecord()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another benefit of doing it this way is that your scripts will still run against the upcoming 1.6 Toolkit release in which the NaErrnoException types have all moved to a new namespace (just "NetApp.Ontapi").&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Great question by the way...&amp;nbsp; I really had to scratch my head on this one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Eric&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 28 Oct 2011 16:43:56 GMT</pubDate>
    <dc:creator>timothyn</dc:creator>
    <dc:date>2011-10-28T16:43:56Z</dc:date>
    <item>
      <title>Exception Syntax</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/Exception-Syntax/m-p/8735#M424</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I can't figure out the Exception syntax for my try/catch statement.&amp;nbsp; I am trying to catch the case of an invalid volume.&amp;nbsp; CWSLSnapInfX does not exist.&amp;nbsp;&amp;nbsp; Here is the current code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;$volName = "CWSQLSnapInfX"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;try&amp;nbsp; {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $hVol = get-naVol -controller $nvController $volName&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "hVol: " + $hVol.Name&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;} &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;catch [NetApp.Ontapi.Filer.ErrnoException] {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host -BackGroundColor Yellow " Netapp UnTrapped Exception...."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Quitting...."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Type: " + $_.Exception.GetType()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;catch [Exception] {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host -BackGroundColor Green "UnTrapped Exception...."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Type: " + $_.Exception.GetType()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Base: " + $_.Exception.GetBaseException()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Only the catch [Exception] block gets run.&amp;nbsp; My output looks like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;UnTrapped Exception....&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Type:&amp;nbsp; + NetApp.Ontapi.Filer.ErrnoException+EVOLUMEDOESNOTEXIST&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Base:&amp;nbsp; + NetApp.Ontapi.Filer.ErrnoException+EVOLUMEDOESNOTEXIST: No volume named 'CWSQLSnapInfX' exists&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp; at NetApp.Ontapi.NaApi`1.Invoke(INaServer server)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp; at DataONTAP.PowerShell.SDK.Cmdlets.Volume.GetNaVol.ProcessRecord()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Error[0]:&amp;nbsp; +&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And, if I replace [NetApp.Ontapi.Filer.ErrnoException] with&amp;nbsp; [Netapp.Ontapi.Filer.ErrnoException+EVOLUMEDOESNOTEXIST] it still does not work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope I am not doing something silly here....&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Jun 2025 06:42:36 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/Exception-Syntax/m-p/8735#M424</guid>
      <dc:creator>dashamalla2</dc:creator>
      <dc:date>2025-06-05T06:42:36Z</dc:date>
    </item>
    <item>
      <title>Re: Exception Syntax</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/Exception-Syntax/m-p/8741#M425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nope, you're not doing anything silly.&amp;nbsp; That's exactly how I would expect it to work too.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PowerShell is doing something silly though.&amp;nbsp; First, EVOLUMEDOESNOTEXIST is a non-terminating error for this cmdlet; you have to set the ErrorAction parameter or $ErrorActionPreference variable otherwise it will just write the error and continue.&amp;nbsp; When you change the ErrorAction to stop, PowerShell wraps the exception in an ErrorRecord object before throwing it down the stack.&amp;nbsp; Obviously "ErrorRecord" won't match the catch type.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are a number of ways to handle this appropriately, but matching the Exception property looks nice to me:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code"&gt;&lt;P&gt;$ErrorActionPreference = "Stop"&lt;/P&gt;&lt;P&gt;$volName = "CWSQLSnapInfX"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;try&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #$res = new-object NetApp.Ontapi.ApiResult&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #throw new-object NetApp.Ontapi.Filer.ErrnoException+EVOLUMEDOESNOTEXIST $res&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $hVol = get-naVol $volName&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "hVol: " + $hVol.Name&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;catch {&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($_.Exception.GetType() -match "EVOLUMEDOESNOTEXIST")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host -BackGroundColor Yellow " Netapp UnTrapped Exception...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Quitting...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host -BackGroundColor Green "UnTrapped Exception...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Type: " $_.GetType().FullName&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Exception: " $_.Exception&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;which outputs this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code"&gt;&lt;P&gt; Netapp UnTrapped Exception....&lt;/P&gt;&lt;P&gt;Quitting....&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Type:&amp;nbsp; System.Management.Automation.ErrorRecord&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Exception:&amp;nbsp; NetApp.Ontapi.ErrnoException+EVOLUMEDOESNOTEXIST: No volume named 'CWSQLSnapInfX' exists&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; at NetApp.Ontapi.NaApi`1.Invoke(INaServer server)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; at DataONTAP.PowerShell.SDK.Cmdlets.Volume.GetNaVol.ProcessRecord()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another benefit of doing it this way is that your scripts will still run against the upcoming 1.6 Toolkit release in which the NaErrnoException types have all moved to a new namespace (just "NetApp.Ontapi").&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Great question by the way...&amp;nbsp; I really had to scratch my head on this one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Eric&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 28 Oct 2011 16:43:56 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/Exception-Syntax/m-p/8741#M425</guid>
      <dc:creator>timothyn</dc:creator>
      <dc:date>2011-10-28T16:43:56Z</dc:date>
    </item>
    <item>
      <title>Exception Syntax</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/Exception-Syntax/m-p/8750#M426</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;dashamalla2,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I had a similar issue when I started using try/catch.&amp;nbsp; For the try/catch code to work, PowerShell must know to stop when an error occurs.&amp;nbsp; This "stop" invokes the try/catch process.&amp;nbsp; If the exception is handled, the script will handle the error and continue running the script. There are two (2) ready methods for doing telling PowerShell to stop on errors..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; Add -ErrorAction Stop to the cmdlet whose error you are trying to catch.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;$hVol = get-naVol -controller $nvController -Names $volName&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;becomes...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;$hVol = get-naVol -controller $nvController -Names $volName -ErrorAction "Stop"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; Set the ErrorAction value for all cmdlets to "Stop"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;$ErrorActionPreference = Stop&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In his script above, Eric Nicholson uses the second method.&amp;nbsp; However, either method will work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please let me know if changing the erroraction fixes the issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Bill&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Nov 2011 20:52:02 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/Exception-Syntax/m-p/8750#M426</guid>
      <dc:creator>paleon</dc:creator>
      <dc:date>2011-11-04T20:52:02Z</dc:date>
    </item>
  </channel>
</rss>

