梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
then mid´level managers。 Corporations and management within corporations understand
that to get anything done�察�they must develop a game plan and then carry out the game plan。
The CEO or highest level manager will know about the overall game plan。 The pany is orga
nized so that lower´level managers and workers can carry out discrete tasks as defined in the
game plan。 In other words�察�these organizational units carry out the implementation of the plan。
Translating this to software�察�your code contains two types of methods�此�methods that orga
nize the functionality and methods that implement the functionality。 You create organizational
code so that you have the ability to separate each task from the others。 The work of one does
not affect the other�察�and thus the code has been modularized。
*Note As with corporation reorganization�察�management code is constantly being reorganized to fix bugs
and implement new pieces of functionality。 For example�察�you might reorganize code so that it bees more
streamlined and efficient。
Now we need to put exceptions into the picture。 Errors occur when something does not go
according to plan。 In the management hierarchy�察�if an error occurs�察�the CEO is not automati
cally told。 For example�察�if the pany ran out of staples�察�most likely the CEO would not want
to hear about it。 If�察�however�察�one of the manufacturing plants fell off the side of a cliff�察�then the
CEO would definitely want to know。 The information about the error is passed up the chain of
mand just as far as it needs to go。
Relating this to the different types of methods�察�in your hierarchical code�察�the implementa
tion code is responsible for making sure all errors are reported�察�and the organizational code is
responsible for either fixing the error or propagating it to a piece of code that is higher in the
hierarchy。 The higher piece of code should either fix the problem or delegate it again to a higher
piece of code�察�depending on the seriousness of the error。
For the remainder of this chapter�察�we¨ll look at the various ways of dealing with exceptions。
The idea is to give you practical solutions that you can use without getting too bogged down in
theoretical what´ifs。
When working with exceptions�察�it is often useful to run the application in Visual Basic¨s
debugger�察�so let¨s start there。
Running the Debugger
The Visual Basic debugger runs the application�察�but adds the ability to monitor what the appli
cation is doing。 To start the debugger�察�select Debug Start Debugging from the menu bar or
press F5。
Your application will run as normal�察�but Visual Basic Express¨s layout will change slightly。
From the perspective of the application�察�you will not see a difference in its behavior。 The appli
cation executes in the same way�察�regardless of whether it is being run or being debugged。 To
stop debugging�察�just close your application normally。
´´´´´´´´´´´´´´´´´´´´´´Page 139´´´´´´´´´´´´´´´´´´´´´´´
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 117
*Tip You may find it useful to display the Locals and Call Stack panes�察�which allow you to see what is going
on in the application。 To do so�察�start debugging and select Debug Windows�察�and then choose the window
��Call Stack or Locals��。
You can also start the debugger at a certain point in your code by setting a breakpoint�察�as
shown in Figure 5´1。 When Visual Basic reaches this breakpoint�察�it goes into debug mode�察�this
mode is different from application mode!the state it is in before it reaches the breakpoint and
after it leaves the breakpoint。 To leave debug mode�察�you can press the F5 key to switch to appli
cation mode and continue executing the application�察�or press Ctrl��Alt��Break to stop debugging
and stop executing the application。
Figure 5´1。 Debugging an application with a breakpoint
Running an application in the debugger is useful when tracking down exceptions�察�as you¨ll
see in the next section。
Handling Exceptions
Those who recall the ^good old days ̄ of Windows 3。0 and its 16 bits will also remember the
dreaded three´finger salute。 The three´finger salute refers to pressing Ctrl��Alt��Delete to reboot
Windows after a program crashed。 You did not have the chance to save your work�察�you just had
to watch everything go down the drain。
If you missed those puting days�察�count yourself lucky。 Nowadays�察�we have mechanisms to
catch unexpected errors and make sure that the program or operating system keeps on processing。
´´´´´´´´´´´´´´´´´´´´´´Page 140´´´´´´´´´´´´´´´´´´´´´´´
118 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
The big deal with modern operating systems and programming environments like the CLR is
that they can stop a single task from disrupting the operation of the CPU。
Catching Exceptions
Recall that in Chapter 2�察�Visual Basic Express interrupted the flow of the program by catching
the exception generated by a mathematical overflow situation。 This is like your driving teacher
realizing you are making a mistake and pressing hard on the brake�察�so you don¨t hit a tree�察�person�察 �
or another car。 So�察�in a sense�察�you can think of the CLR exception´handling mechanism as the
teacher stepping on the brake when something devastating is about to happen。
Stepping on the brake stops the devastation�察�but you�察�as the driver�察�will get a shock because
of the error that you were about to make。 Likewise�察�when an exception is triggered�察�the program
gets a shock。 How you deal with the shock determines the fate of the program。 In the example
of the overflow error in Chapter 2�察�the shock was captured by the IDE�察�and thus a friendly�察�easy
to´understand user interface was presented。
Consider the source code shown in Figure 5´2�察�which generates an exception。 This is referred
to as throwing an exception。
Class declaration with a
single data member
Class is declared�察�but
Class MyType
Public DataMember As Integer not instantiated
End Class
Class Tests
Public Sub GeneratesException�┌�
Dim cls As MyType = Nothing
cls。DataMember = 10
End Sub
Public Sub RunAll�┌� Data member of the class is assigned even
GeneratesException�┌� though the class has not been instantiated。
End Sub
This will cause an exception。
End Class
Figure 5´2。 Throwing an exception
If the RunAll�┌� method were executed�察�the exception shown in Figure 5´3 would be generated
in Visual Basic Express ��use F5 to run the debugger��。
The exception did not cause the operating system or Visual Basic Express to crash�察�because
Visual Studio¨s built´in exception handler caught it。 Visual Studio hit the brake and made sure
that only yo