try... catch... not firing

Ask your 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.
This topic is 4 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Rixtter747
Posts: 11
Last visit: Tue Nov 08, 2022 2:44 am

try... catch... not firing

Post by Rixtter747 »

Hi folks,

I have several try... catch statements in my code, working well until this latest attempt.
  1.     try {
  2.         Get-Mailbox $searchCriteria
  3.         $txtInfo.Text = Get-Mailbox $searchCriteria | select name, database
  4.         $radMoveToSmallest.Enabled = $true
  5.         $radMoveToSpecific.Enabled = $true
  6.     }
  7.     catch {
  8.         $txtInfo.Text = "Error detected:`n"
  9.         $txtInfo.Text += $($_.exception.message)
  10.     }
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: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

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

Post by Alexander Riedel »

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: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

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

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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
byrnecutIT
Posts: 4
Last visit: Fri Jan 03, 2020 12:41 am

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

Post by byrnecutIT »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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: 11
Last visit: Tue Nov 08, 2022 2:44 am

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

Post by Rixtter747 »

Alexander Riedel wrote: Wed Dec 11, 2019 8: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: 11
Last visit: Tue Nov 08, 2022 2:44 am

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

Post by Rixtter747 »

jvierra wrote: Wed Dec 11, 2019 10: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: 11
Last visit: Tue Nov 08, 2022 2:44 am

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

Post by Rixtter747 »

daveyc wrote: Wed Dec 11, 2019 5: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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Rixtter747 wrote: Thu Dec 12, 2019 2: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.
This topic is 4 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked