As mentioned already, if an error occurs when processing the program in the Try section, the compiler transfers the processing to the next Catch section. You can then use the catch section to deal with the error. At a minimum, you can display a message to inform the user. To do this, you can create a message box in the Catch section. Here is an example: Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private LblNumber As Label
Private TxtNumber As TextBox
Friend WithEvents BtnCalculate As Button
Private LblResult As Label
Private TxtResult As TextBox
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
Text = "Exception Behavior"
LblNumber = New Label
LblNumber.Location = New Point(17, 23)
LblNumber.Text = "Number:"
LblNumber.AutoSize = True
TxtNumber = New TextBox
TxtNumber.Location = New Point(78, 20)
TxtNumber.Size = New Size(83, 20)
BtnCalculate = New Button
BtnCalculate.Location = New Point(78, 45)
BtnCalculate.Text = "Calculate"
BtnCalculate.Size = New Size(83, 23)
LblResult = New Label
LblResult.Location = New Point(17, 75)
LblResult.Text = "Result:"
LblResult.AutoSize = True
TxtResult = New TextBox
TxtResult.Location = New Point(76, 72)
TxtResult.Size = New Size(83, 20)
Controls.Add(LblNumber)
Controls.Add(TxtNumber)
Controls.Add(BtnCalculate)
Controls.Add(LblResult)
Controls.Add(TxtResult)
End Sub
Private Sub CalculateClicked(ByVal sender As Object,
ByVal e As EventArgs)
Handles BtnCalculate.Click
Dim Number As Double
Dim Result As Double
Try
Number = CDbl(TxtNumber.Text)
Result = Number * 12.48
TxtResult.Text = CStr(Result)
Catch
MsgBox("Something bad happened")
End Try
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
Of course, your message may not be particularly clear but this time, the program will not crash.
Most libraries such as Borland's VCL and Microsoft's MFC ship with their own classes to handle exceptions. Even the Win32 library provides its type of mechanism to face errors. To support exception handling, the .NET Framework provides a special class called Exception. Once the compiler encounters an error, the Exception class allows you to identify the type of error and take an appropriate action. Normally, Exception mostly serves as the general class of exceptions. It is used like a Catch that is not followed by any parameter. Anticipating various types of problems that can occur in a program, Microsoft derived various classes from Exception to make this issue friendlier. As a result, almost any type of exception you may encounter already has a class created to deal with it. Therefore, when your program faces an exception, you can easily identify the type of error. There are so many exception classes that we cannot study or review them all. The solution we will use is to introduce or review a class when we meet its type of error. In exception handling, errors are dealt with in the Catch clause. To use it, on the right side of Catch, type a parameter name, followed by the As operator, and followed by the the type of exception you want to deal with. By default, an exception is first of type Exception. Based on this, a typical formula to implement exception handling is: Private Sub CalculateClicked(ByVal sender As Object,
ByVal e As EventArgs)
Handles BtnCalculate.Click
Dim Number As Double
Dim Result As Double
Try
' Process the normal flow of the program here
Catch ex As Exception
' Deal with the exception here
End Try
End Sub
As reviewed already, when an exception occurs in the Try section, code compilation is transferred to the Catch section. If you declare the exception as an Exception type, this class will identify the error.
One of the properties of the Exception class is called Message. This property contains a string that describes the type of error that occurred. You can then use this Exception.Message property to display an error message if you want. Here is an example: Private Sub CalculateClicked(ByVal sender As Object,
ByVal e As EventArgs)
Handles BtnCalculate.Click
Dim Number As Double
Dim Result As Double
Try
Number = CDbl(TxtNumber.Text)
Result = Number * 12.48
TxtResult.Text = CStr(Result)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
As you can see, one of the strengths of the Message property is that it gives you a good indication of the type of problem that occurred. Sometimes, the message provided by the Exception class may not appear explicit enough. In fact, you may not want to show it to the user since, as in this case, the user may not understand what the message means and why it is being used. As an alternative, you can create your own message and display it to the user. As seen previously, to display your own message, in the Catch section, use the MsgBox() function to create and display a message. Here is an example: Private Sub CalculateClicked(ByVal sender As Object,
ByVal e As EventArgs)
Handles BtnCalculate.Click
Dim Number As Double
Dim Result As Double
Try
Number = CDbl(TxtNumber.Text)
Result = Number * 12.48
TxtResult.Text = CStr(Result)
Catch ex As Exception
MsgBox("The operation could not be carried because " &
"the number you typed is not valid")
End Try
End Sub
You can also combine the Exception.Message message and your own message: Private Sub CalculateClicked(ByVal sender As Object,
ByVal e As EventArgs)
Handles BtnCalculate.Click
Dim Number As Double
Dim Result As Double
Try
Number = CDbl(TxtNumber.Text)
Result = Number * 12.48
TxtResult.Text = CStr(Result)
Catch ex As Exception
MsgBox(ex.Message &
vbCrLf & "The operation could not be carried because " &
"the number you typed is not valid")
End Try
End Sub
|
|
|||||||||||||||
|
|