Shane Partridge
Visual Basic 2013
The Code
Public Class Form95
'## The new approach to Handling Errors or rather Exception Handling ------------------##
'## It is supposed to be more efficient than the old [On Error Goto] method -----------##
'## Where it can handle various types of errors with [Try... Catch... End Try] structure -----##
Private Sub ErrorException_Click(sender As Object, e As EventArgs) Handles Button1.Click
'## Declare Lbl_ErrMsg as Invisible --------------------##
Lbl_ErrMsg.Visible = False
'## Reset Lbl_Answer.Text Forecolor to Blue ------------##
Lbl_Answer.ForeColor = Color.Blue
'## Dimension Variables as Double Precision [Numeric]--- ##
Dim firstNum, secondNum, answer As Double
'## Try... Catch... End Try... Stucture -----------------##
Try
'## Declare Variables to Text.Box User Input -----------##
firstNum = TxtNum1.Text
secondNum = TxtNum2.Text
'## Mathematical Equation ---------------------------##
answer = firstNum / secondNum
Lbl_Answer.Text = answer
'## Catch Non_Numeric Exception ---------------------##
Catch Non_Numeric As Exception
'## Subroutine[1] NonNumeric() ----------------------##
NonNumeric()
End Try
End Sub
'## Sub[1] ---------------------------------------------------##
'## Error Handling Message Routine ---------------------------##
Sub NonNumeric()
Lbl_Answer.ForeColor = Color.Red
Lbl_Answer.Text = "Error"
'## Declare Lbl_ErrMsg as Visible ------------------------##
Lbl_ErrMsg.Visible = True
Lbl_ErrMsg.ForeColor = Color.Red
Lbl_ErrMsg.Text = " One of the entries is not a number! Try again!"
End Sub
Private Sub InitialStartup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'## Initial Startup Handles MyBase.Load ##
'## Declare Labels as Invisible ------- ##
Lbl_ErrMsg.Text = ""
Lbl_Answer.Text = ""
End Sub
End Class