Return statement sometimes not working

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 8 years and 1 month 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
User avatar
Lembasts
Posts: 405
Last visit: Wed Mar 20, 2024 1:40 pm
Has voted: 1 time
Been upvoted: 1 time

Return statement sometimes not working

Post by Lembasts »

Greetings,
I have a large application with about 15 forms.
Each of these forms has a click event on it which processes information provided on the form.
These click events often have quite a number of try/catch blocks in them.
In many cases the catch block writes an error to a logfile and to the window and issues a return statement e.g.:

try
{
Add-MailboxPermission -Identity $Mailbox.distinguishedname -User $Group.distinguishedname -AccessRight FullAccess -InheritanceType All -DomainController $ActiveSiteDC -ErrorAction Stop
}
Catch
{
Catcherror $_ "ERROR: Granting Full Access to [$MBGroupName]" $Mailbox
$formCreateMailboxes.cursor = "default"
return
}

The user running the application sees the error message and is free to fix the problem and continue. So in most cases the Return statement simply does what it should and goes to the end of the click event function and returns control to the end user.
However....
In one form, the return statements simply exit the catch block and continue processing the click event function code.
Why would this be?

Thanks
David Z
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Return statement sometimes not working

Post by jvierra »

There is really no way for anyone to guess at what Is happening. This usually happens because of a subtle syntax error somewhere in the code before the bad bit. As a programmer I have been driven crazy by this kind of thing. When I finally learned how to approach code building this kind of issue almost totally disappeared.

The way to fix this or to find the bad code is to start by removing bits of code until the problem stops then inspect the removed code very carefully. THat is what I call the brute force method. It doesn't always work but it works most of the time.

The biggest failure type is getting unprintable characters stuck in the source file. This happens often when copying and pasting code from the web. I use PrimalScript to look for bad characters and nulls.

Good luck. Sorry I can't hand you a magic bullet.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Return statement sometimes not working

Post by jvierra »

The first thing I would suspect is this line:

Catcherror $_ "ERROR: Granting Full Access to [$MBGroupName]" $Mailbox

Put a logging trace statement in the function at entry and another just before it exits. When this happens inspect the tract file. The function may not be the cause but it may be the inheritor of the issue. This may be more true if the function is in n external file.
User avatar
Lembasts
Posts: 405
Last visit: Wed Mar 20, 2024 1:40 pm
Has voted: 1 time
Been upvoted: 1 time

Re: Return statement sometimes not working

Post by Lembasts »

Thanks. In all 15 forms the catcherror call (which is a function in globals.ps1) precedes each return statement.
I get the bit about non printable characters and Im not looking forward to trying to find it :-(.

Cheers

David
This topic is 8 years and 1 month 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