try... catch... not firing

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
Rixtter747
Posts: 10
Joined: Fri Nov 29, 2019 2:34 am

try... catch... not firing

Post by Rixtter747 » Wed Dec 11, 2019 8:51 am

Hi folks,

I have several try... catch statements in my code, working well until this latest attempt.
  1.     try {
  2.  
  3.         Get-Mailbox $searchCriteria
  4.  
  5.         $txtInfo.Text = Get-Mailbox $searchCriteria | select name, database
  6.  
  7.         $radMoveToSmallest.Enabled = $true
  8.  
  9.         $radMoveToSpecific.Enabled = $true
  10.  
  11.     }
  12.  
  13.     catch {
  14.  
  15.         $txtInfo.Text = "Error detected:`n"
  16.  
  17.         $txtInfo.Text += $($_.exception.message)
  18.  
  19.     }
I put Line 2 in there for simplicity, to see if in Debug mode would fire on a cut-down version of Line 3.

So I input an entry that doesn't exist in our GAL, so "catch" should fire immediately. I can even see that the code has generated an error in the Output window when I am debugging, yet it carries on firing the remaining code in the "try" statement and completely ignores the "catch" code.

I have similar working perfectly in my 2nd form. This is the 3rd form I have added. Limitation on form creation?

Honestly, I'm getting frustrated with the bugs in this software. Can I obtain version 2018 instead of 2019? I need some stable software, I don't have time to work with what clearly seems like a Beta.

User avatar
Alexander Riedel
Posts: 7214
Joined: Tue May 29, 2007 4:43 pm

Re: try... catch... not firing

Post by Alexander Riedel » Wed Dec 11, 2019 9:28 am

Alright, let me first state that I understand your frustration. Every software developer hates little more than code working in one spot but not the other for no apparent reason.

But, let me address this here as clearly as possible, since this keeps coming up.

Windows Forms is part of the Microsoft .NET framework. It is not a SAPIEN product.
Windows PowerShell is part of Microsoft Windows, thereby not a SAPIEN product.

We are providing the IDE, generate the code and few other things, so if there is a problem in that, we are happy to look and address it.

However, Get-Mailbox throwing an exception and Microsoft Windows PowerShell not catching it? Not our corner of this.
Using PowerShell Studio 2018 would neither change the .NET framework nor the Windows PowerShell engine your code is running in.

To make that ample clear, WE as in SAPIEN Technologies, have no (zip, zero, zilch) influence as to what exceptions Windows PowerShell catches and which not, whether they come from Windows Forms or elsewhere, as in this case. Chances are, there is actually no exception thrown in your case. Just seeing error output (which you didn't provide) doesn't necessarily mean there is an exception thrown.

I will move this post into the general PowerShell section, since it is not related to PowerShell Studio.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
Alexander Riedel
Posts: 7214
Joined: Tue May 29, 2007 4:43 pm

Re: try... catch... not firing

Post by Alexander Riedel » Wed Dec 11, 2019 9:29 am

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

jvierra
Posts: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: try... catch... not firing

Post by jvierra » Wed Dec 11, 2019 11:11 am

Hi rixtter. Your issue is an issue with how to use the exception handling in PowerShell and has noting to do wit PSS.

I suggest starting with the help for try/catch:

help about_try_catch

To cause a trappable event to work in a try/catch it is necessary to add "-ErrorAction Stop" to each command that you want to be trapped.

Get-Mailbox $searchCriteria -ErrorAction Stop

daveyc
Posts: 4
Joined: Thu Jan 31, 2019 9:25 am

Re: try... catch... not firing

Post by daveyc » Wed Dec 11, 2019 6:18 pm

Remember that a try/catch will only work on terminating errors. As jvierra mentioned, you can add the -erroraction to a cmdlet or you can set all errors to terminate by using the $erroractionpreference variable.

jvierra
Posts: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: try... catch... not firing

Post by jvierra » Wed Dec 11, 2019 6:26 pm

I recommend against global setting of the $ErroractionPreference variable for obvious reasons however, in a function, this is a simple method. The setting of the variable in a function is local only to the function. You can also set it in the beginning of the try block and clear it in the "Finally" block which will guarantee that it is reset for the session or the function only within the Try/Catch block.

Rixtter747
Posts: 10
Joined: Fri Nov 29, 2019 2:34 am

Re: try... catch... not firing

Post by Rixtter747 » Thu Dec 12, 2019 3:37 am

Alexander Riedel wrote:
Wed Dec 11, 2019 9:28 am
Alright, let me first state that I understand your frustration. Every software developer hates little more than code working in one spot but not the other for no apparent reason.

But, let me address this here as clearly as possible, since this keeps coming up.

Windows Forms is part of the Microsoft .NET framework. It is not a SAPIEN product.
Windows PowerShell is part of Microsoft Windows, thereby not a SAPIEN product.

We are providing the IDE, generate the code and few other things, so if there is a problem in that, we are happy to look and address it.

However, Get-Mailbox throwing an exception and Microsoft Windows PowerShell not catching it? Not our corner of this.
Using PowerShell Studio 2018 would neither change the .NET framework nor the Windows PowerShell engine your code is running in.

To make that ample clear, WE as in SAPIEN Technologies, have no (zip, zero, zilch) influence as to what exceptions Windows PowerShell catches and which not, whether they come from Windows Forms or elsewhere, as in this case. Chances are, there is actually no exception thrown in your case. Just seeing error output (which you didn't provide) doesn't necessarily mean there is an exception thrown.

I will move this post into the general PowerShell section, since it is not related to PowerShell Studio.
Alexander, I very much appreciate your taking the time to reply. I can only imagine the frustration of having to repeat yourself when a new idiot such as myself joins and gets this wrong :oops: :D

Thank you very much, I understand and I am sorry for venting. I love coding, unfortunately I do tend to go off the deep end when I can't figure out a workaround though :oops:

The advice here is fantastic though, I'll reply to each kind enough to help me here.

PS Studio rox 8-)

Rixtter747
Posts: 10
Joined: Fri Nov 29, 2019 2:34 am

Re: try... catch... not firing

Post by Rixtter747 » Thu Dec 12, 2019 3:43 am

jvierra wrote:
Wed Dec 11, 2019 11:11 am
Hi rixtter. Your issue is an issue with how to use the exception handling in PowerShell and has noting to do wit PSS.

I suggest starting with the help for try/catch:

help about_try_catch

To cause a trappable event to work in a try/catch it is necessary to add "-ErrorAction Stop" to each command that you want to be trapped.

Get-Mailbox $searchCriteria -ErrorAction Stop
Thank you jvierra, this has solved my problem :lol: 8-)

Oddly, it works with Line 2, the condensed version & not on Line 3. No biggie, I can kinda figure that but thought I would mention it for anyone else reading...

Rixtter747
Posts: 10
Joined: Fri Nov 29, 2019 2:34 am

Re: try... catch... not firing

Post by Rixtter747 » Thu Dec 12, 2019 3:46 am

daveyc wrote:
Wed Dec 11, 2019 6:18 pm
Remember that a try/catch will only work on terminating errors. As jvierra mentioned, you can add the -erroraction to a cmdlet or you can set all errors to terminate by using the $erroractionpreference variable.
Thanks daveyc, another interesting option although I'll keep it local just to be safe.

I think the issue here, is that I am importing the Exchange snappin and powershell isn't quite on the same page to register the error without explicitly adding the -erroraction switch. I'm now happy to proceed 8-) :D

jvierra
Posts: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: try... catch... not firing

Post by jvierra » Thu Dec 12, 2019 3:54 am

Rixtter747 wrote:
Thu Dec 12, 2019 3:46 am
Thanks daveyc, another interesting option although I'll keep it local just to be safe.

I think the issue here, is that I am importing the Exchange snappin and powershell isn't quite on the same page to register the error without explicitly adding the -erroraction switch. I'm now happy to proceed 8-) :D
The Try/Catch will only work when the ErrorAction is set to Stop. That is a hard requirement. For Net calls to the Net API this is not required. It is critical that you understand the design and requirements for using this facility in PowerShell.

Locked