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
Return statement sometimes not working
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.
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.
Re: Return statement sometimes not working
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.
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.
Re: Return statement sometimes not working
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.
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.
Re: Return statement sometimes not working
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
I get the bit about non printable characters and Im not looking forward to trying to find it .
Cheers
David