Try catch Form

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
balkanweb
Posts: 16
Joined: Wed Apr 01, 2009 7:14 am

Try catch Form

Post by balkanweb » Thu Aug 23, 2012 5:34 am

I like to terminate script if Connection cannot be performed.

try {
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri Http://server/powerShell/ -Authentication Kerberos -Credential (Get-Credential) -ea 'Stop' }

Catch {
Write-Host "Failure when connecting to remote server."
exit
}

when this runs i get an Unhandled Exception and Forms continues to load.

How can i terminate script if $session fails ?

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Try catch Form

Post by davidc » Thu Aug 23, 2012 5:48 am

It is not recommended to use exit keyword in a form. Use the form's Close method: $form1.Close()return #If you are in a function / event See the Spotlight on the Form Control blog article for more details on the Form control. David
David
SAPIEN Technologies, Inc.

User avatar
balkanweb
Posts: 16
Joined: Wed Apr 01, 2009 7:14 am

Try catch Form

Post by balkanweb » Thu Aug 23, 2012 8:00 am

I did try the form.close() but it didn't work (script would continue to run lines after catch{}.

Adding the Return fixed it :)

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Try catch Form

Post by davidc » Thu Aug 23, 2012 9:47 am

The Close method doesn't stop the script. It only signals the form to close (once it can process the message). David
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Try catch Form

Post by davidc » Thu Aug 23, 2012 10:00 am

The following blog should hopefully help you understand what is happening with the form controls and events: http://www.sapien.com/blog/2011/07/05/p ... on-clicks/ David
David
SAPIEN Technologies, Inc.