梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
Visual Studio¨s built´in exception handler caught it。 Visual Studio hit the brake and made sure
that only your program stopped working。
Imagine if Visual Studio were not running。 The generated exception would cause the program
to stop in its tracks�察�and a messy error message would appear�察�referencing objects�察�line numbers�察 �
and the stack。 Most users would have no idea what happened�察�and be left with a program that
was not working anymore。
´´´´´´´´´´´´´´´´´´´´´´Page 141´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 5 * L E AR N IN G AB O U T V I SU A L B AS IC E X CE PT I ON HA N D L IN G 119
Figure 5´3。 An exception generated by a null reference
What you want to do is catch the exception as Visual Studio did。 For example�察�if you knew
that an exception could occur in RunAll�┌��察�you could modify the code as follows�此�
Class MyType
Public DataMember As Integer
End Class
Class Tests
Public Sub GeneratesException�┌�
Dim cls As MyType = Nothing
cls。DataMember = 10
End Sub
Public Sub RunAll�┌�
Try
GeneratesException�┌�
Catch exception1 As Exception
End Try
End Sub
End Class
The bolded code is an exception block�察�which catches an exception and allows you to respond
to it。 In this example�察�nothing happens after catching the exception。 If you run this program�察 �
Visual Basic will not generate an exception warning�察�and the program will run without any
problems。 From the perspective of Visual Basic�察�everything worked and is running OK。
But if you stop and think about it�察�is everything really OK�拭�Even though the program
continued to execute�察�was the program logically correct�拭�The answer is no�察�because what the
program did was swallow an exception without doing anything to remedy the problem。 You
should not do such a thing�察�because it implies sloppy programming。
*Note In practice�察�there are some cases when you need to swallow errors because you cannot process the
data in any other way。 This can happen when you are dealing with network connections�察�database operations�察 �
and the like。 However�察�in the majority of cases�察�you should not swallow the exception。
´´´´´´´´´´´´´´´´´´´´´´Page 142´´´´´´´´´´´´´´´´´´´´´´´
120 CH AP T E R 5 * L E A R N IN G AB OU T V I SU A L B AS IC E X C E P TI ON H AN D L IN G
A real´world example of when you might want to throw an exception is because a param
eter is not valid。 As you will learn in the ^Filtering Exceptions ̄ section later in this chapter�察�there is
an exception type for just this purpose�察�ArgumentException�┌�。 Developers who received that
exception would then be able to easily figure out that they must fix a parameter。 This saves
debugging time and reduces overall development time。
The real work in exception handling is adding all of the possible exceptions that could
occur。 But what is better�此�hunting down the cause of the error or adding the code to help you
find the error easily�拭�In the long run�察�adding the code to help track down the error saves time
and avoids frustration。
Implementing Exception Handlers
An exception handler is implemented using the Try�察�Catch�察�and Finally keywords。 When you
implement an exception handler�察�you are saying that for a specific code block�察�if an exception
is generated�察�you will catch it and process the exception。
An exception handler has the following form�此�
��action 1�А�
Try
��action 2�А�
Catch exception1 As Exception
��action 3�А�
End Try
��action 4�А�
The notation of an exception handler is simple。 The Try and Catch keywords define the
boundaries of a code block that is protected。 Protected means that any generated exception will
need to pass through the exception handler。 If an exception is thrown by the protected code�察 �
the code in the Catch block will execute and allow you to process the exception。 Catch is the
keyword defined immediately after the Try block�察�and the Catch block specifies which excep
tion is caught and defines the steps to take when an exception occurs。
If code within the Try block ��action 2 in the example�� calls another method�察�then the code
within the called method is protected。 This is the case even if the code is not protected from the
perspective of the method。 Figure 5´4 illustrates how exceptions are caught when multiple
methods are involved。
In Figure 5´4�察�the protected code of action 2 calls a method that has action 2。1�察�action 2。2�察 �
and action 2。4。 action 2。2 executes in the context of an exception block�察�and thus if action 2。2
throws an exception�察�it will be caught and processed by action 2。3。 The Catch block containing
action 3 is not aware an exception occurred。 From the perspective of the called method�察�action
2。1 and action 2。4 are not protected�察�but because the called method is called from action 2�察�which
is protected by the Catch block containing action 3�察�action 2。1 and action 2。4 are considered
protected。 If action 2。1 or action 2。4 had thrown an exception�察�then the Catch block with
action 3 would have caught the exception。 This example illustrates two things�此 �
o Exceptions can span method calls。
o When an exception is thrown�察�the handler will catch it closest to the place where the
exception occurred。
´´´´´´´´´´´´´´´´´´´´´´Page 143´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 5 * L E AR N IN G AB O U T V I SU A L B AS IC E X CE PT I ON HA N D L IN G 121
Figure 5´4。 Exceptions and calling methods that generate exceptions
An example of how exceptions function is our legal court system。 Let¨s say that you want
to slap someone with a lawsuit。 If the lawsuit is a civil case�察�it will be heard by the local court。
You cannot bring a civil lawsuit directly to the supreme court level until it has been heard by
the lower court levels。 If the lawsuit is a criminal case�察�most likely it will be heard at the state/
provincial level first。 The local municipality will not hear the case because it is not in the juris
diction of that court。
The same behavior occurs with exceptions and exception handlers。 An exception might be
caught and processed by one exception handler�察�without the exception being processed by a
higher´level exception handler。 This situation is illustrated in Figure 5´3 where an exception
was thrown and caught by the IDE。 An exception that was thrown several method calls deep
was caught at the highest level。
In previous examples�察�an exception was thrown because the code did something that
it was not supposed to do。 However�察�you can throw an exception on purpose by using the
following syntax�此�
Throw New Exception�┌�
When you throw an exception like this�察�you instantiate a type that is related to the base
type Exception。 Associating the Throw keyword with an object creates an exception that can be
caught by a Catch block。
In most cases of throwing an exception�察�you will instantiate the exception type as you throw it。
The previous example uses the parameterless Exception�┌� constructor�察�but other variations
are available�察�as shown in the following code。
Try
Throw New Exception�─�Exception in action 2。4。;��
Catch thrown As Exceptio