bob500000
bob500000

Reputation: 103

Capturing unhandled exceptions

I am trying to catch a unhandlled exception that powershell produces using windows forms;

Currently I am able to capture terminating errors using try/catch and have researched several post and still it shows the unhandled exception error box, which I would like to not show up.

Try Catch, doesn't seem to help

Function Get-Choice{
Try{
$ErrorActionPreference = 'Stop'
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$Form3 = New-Object System.Windows.Forms.Form

$Form3.Text = "Please Make A Choice From Below"

$Form3.ClientSize = New-Object System.Drawing.Size(390, 160)

$form3.topmost = $true

$Text = New-Object System.Windows.Forms.Label

$Text.Location = New-Object System.Drawing.Point(15, 15)

$Text.Size = New-Object System.Drawing.Size(300, 80)

$Text.Text = "What would you like to do today? `n`n 1. Find the Permissions of a folder `n 2. Find Who Has Access to a Folder `n 3. Find out what shares are on a server`n 4. Select a csv that contains a list of shares"

$Form3.Controls.Add($Text)

#$ErrorActionPreference = "SilentlyContinue"

Function Button1

{

$Button1 = New-Object System.Windows.Forms.Button

$Button1.Location = New-Object System.Drawing.Point(15, 100)

$Button1.Size = New-Object System.Drawing.Size(125, 25)

$Button1.Text = "1. Folder Permissions"

$Button1.add_Click({Folder

$Form3.Close()})

$Form3.Controls.Add($Button1)

}

Function Button2

{

$Button2 = New-Object System.Windows.Forms.Button

$Button2.Location = New-Object System.Drawing.Point(145, 100)

$Button2.Size = New-Object System.Drawing.Size(100, 25)

$Button2.Text = "2. Folder Access"

$Button2.add_Click({Members 

$Form3.Close()})

$Form3.Controls.Add($Button2)

}

Function Button3

{

$Button3 = New-Object System.Windows.Forms.Button

$Button3.Location = New-Object System.Drawing.Point(250, 100)

$Button3.Size = New-Object System.Drawing.Size(130, 25)

$Button3.Text = "3. Shares on a Server"

$Button3.add_Click({Shares 

$Form3.Close()})

$Form3.Controls.Add($Button3)

}

Function Button4

{

$Button4 = New-Object System.Windows.Forms.Button

$Button4.Location = New-Object System.Drawing.Point(145, 128)

$Button4.Size = New-Object System.Drawing.Size(100, 25)

$Button4.Text = "4. Auto Shares"

$Button4.add_Click({Auto 

$Form3.Close()})

$Form3.Controls.Add($Button4)

}

Button1

Button2

Button3

Button4

[void]$Form3.showdialog()
}
Catch[System.Exception]{
}
}
}
Get-Choice

The Unhandled exception dialog is not shown but the scripts terminates

Upvotes: 0

Views: 267

Answers (2)

bob500000
bob500000

Reputation: 103

So solved this issue by using return instead of exit on code and written to try catch,

Thanks for your assistance @T-Me

Upvotes: 1

T-Me
T-Me

Reputation: 1884

Enclose the funcions you call in the Click events in try-catch-Blocks to "handle" the exception:

Function Button1
{
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Point(15, 100)
$Button1.Size = New-Object System.Drawing.Size(125, 25)
$Button1.Text = "1. Folder Permissions"
$Button1.add_Click({
Try{
    Folder
}catch{<# Maybe something here #>}
$Form3.Close()})
$Form3.Controls.Add($Button1)
}

Upvotes: 0

Related Questions