I've designed a big, clunky gui with buttons and other controls full of "risky operations"...code that is capable of throwing an exception either specifically designed or baked in at a lower level.
Code: Select all
Gui, New
Gui, Add, Button, gButton1 vButton1 w200, Button 1
Gui, Add, Button, gButton2 vButton2 w200, Button 2
Gui, Add, Button, gButton3 vButton3 w200, Button 3
Gui, Add, Button, gButton4 vButton4 w200, Button 4
Gui, Add, Button, gButton5 vButton5 w200, Button 5
Gui, Show
return
Button1:
Throw, Exception("You pressed " A_GuiControl, 0, "Why would you do that?!")
return
Button2:
Throw, Exception("I might have expected this exception.", 0, "Because I designed it myself.")
return
Button3:
risky_operation()
return
Button4:
borrowed_function()
return
Button5:
msgBox % "There is nothing wrong with clicking this button."
return
Guiclose:
GuiEscape:
ExitApp
risky_operation()
{
Random, random_number, 0, 1
if random_number
{
Throw, Exception("You knew this was a posibility!")
}
else
{
msgBox % "Nothing to see here."
}
}
borrowed_function()
{
; ...
; code formatted according to someone else's standards
; ...
Throw, Exception("Hah! Gotcha.")
; ...
}
Code: Select all
; ...
Try
{
Button1:
Throw, Exception("You pressed " A_GuiControl, 0, "Why would you do that?!")
return
Button2:
Throw, Exception("I might have expected this exception.", 0, "Because I designed it myself.")
return
Button3:
risky_operation()
return
Button4:
borrowed_function()
return
Button5:
msgBox % "There is nothing wrong with clicking this button."
return
}
Catch e
{
; email it to the helpdesk
msgBox % "I'm so clever."
}
; ...
Code: Select all
; ...
Button1:
Try
{
Throw, Exception("You pressed " A_GuiControl, 0, "Why would you do that?!")
}
Catch e
{
email_helpesk_exception(e)
}
return
Button2:
Try
{
Throw, Exception("I might have expected this exception.", 0, "Because I designed it myself.")
}
Catch e
{
email_helpesk_exception(e)
}
return
Button3:
Try
{
risky_operation()
}
Catch e
{
email_helpesk_exception(e)
}
return
Button4:
Try
{
borrowed_function()
}
Catch e
{
email_helpesk_exception(e)
}
return
Button5:
msgBox % "There is nothing wrong with clicking this button."
return
; ...
What I envision is a directive similar to OnMessage...Somewhere I can specify a function to run any time an exception goes uncaught.
Code: Select all
OnException("Email_Helpdesk_Exception")
Gui, New
Gui, Add, Button, gButton1 vButton1 w200, Button 1
Gui, Add, Button, gButton2 vButton2 w200, Button 2
Gui, Add, Button, gButton3 vButton3 w200, Button 3
Gui, Add, Button, gButton4 vButton4 w200, Button 4
Gui, Add, Button, gButton5 vButton5 w200, Button 5
Gui, Show
return
Button1:
Throw, Exception("You pressed " A_GuiControl, 0, "Why would you do that?!")
return
Button2:
Throw, Exception("I might have expected this exception.", 0, "Because I designed it myself.")
return
Button3:
risky_operation()
return
Button4:
borrowed_function()
return
Button5:
msgBox % "There is nothing wrong with clicking this button."
return
Guiclose:
GuiEscape:
ExitApp
risky_operation()
{
Random, random_number, 0, 1
if random_number
{
Throw, Exception("You knew this was a posibility!")
}
else
{
msgBox % "Nothing to see here."
}
}
borrowed_function()
{
; ...
; code formatted according to someone else's standards
; ...
Throw, Exception("Hah! Gotcha.")
; ...
}
Email_Helpdesk_Exception(p_exception)
{
; magic happens here
}