Support Forums

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.
User avatar
Lembasts
Posts: 121
Joined: Thu Mar 15, 2012 9:47 am

Return statement sometimes not working

Postby Lembasts » Thu Feb 11, 2016 5:29 pm

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
User avatar
jvierra
Posts: 9736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Return statement sometimes not working

Postby jvierra » Thu Feb 11, 2016 5:43 pm

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.
User avatar
jvierra
Posts: 9736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Return statement sometimes not working

Postby jvierra » Thu Feb 11, 2016 5:46 pm

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: 121
Joined: Thu Mar 15, 2012 9:47 am

Re: Return statement sometimes not working

Postby Lembasts » Thu Feb 11, 2016 7:18 pm

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

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 5 guests