嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及46嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��






Try 

    Throw New Exception�─�Exception in action 2。4。;�� 

Catch thrown As Exception 

    Throw New Exception�─�Exception in action 2 has been caught。;�察�thrown�� 

End Try 


´´´´´´´´´´´´´´´´´´´´´´Page 144´´´´´´´´´´´´´´´´´´´´´´´

122       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 first variation�察 �Exception�─�Exception in action 2。4。;���察�uses the string description  

           constructor parameter that passes text describing what went wrong。 The text is meant for human  

          understanding�察�so don¨t use text like ^Error 168�此�something went wrong。 ̄ The second variation�察 �

           Exception�─�Exception in action 2 has been caught。;�察�thrown���察�includes the original excep

          tion as an additional constructor parameter in a newly thrown exception。 This allows you to  

          pass on even more information。 

                The generated output of this code looks like this�此�



           Unhandled Exception�此�System。Exception�此�Exception in action 2 has been caught。 

           ´´´〃 System。Exception�此�Exception in action 2。4。 



                The generated exception tells you clearly where the exception occurred and where it was  

          processed。 You have a plete flow of the actions。  



          *Note  An experienced developer may say that the flow is also presented by the stack that is dumped by  

          the program if the exception is not caught。 Yes�察�you could see the program flow based on this stack dump�察 �

           but deciphering a stack dump where you have a stack of say 10 or 15 is not much fun to figure out or read。 



                Consider the following amendment to the code�察�which reduces the amount of information。 



          Try 

              Throw New Exception�─�Exception in action 2。4。;�� 

          Catch exception1 As Exception 

              Throw New Exception�─�Exception in action 2 has been caught;�� 

           End Try 



                The results are not as enlightening�此�



           Unhandled Exception�此�System。Exception�此�Exception in action 2 has been caught。 



                If you want to gain access to the error string�察�you can use the Message property of an exception。 



          Try 

              Throw New Exception�─�Exception in action 2。4。;�� 

          Catch thrown As Exception 

              Console。WriteLine��thrown。Message�� 

              Throw New Exception�─�Exception in action 2 has been caught。;�� 

           End Try 



                You still have the more specific message�察�but not as part of the flow of exceptions�此�


´´´´´´´´´´´´´´´´´´´´´´Page 145´´´´´´´´´´´´´´´´´´´´´´´

                                    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 123 



Exception in action 2。4。 



Unhandled Exception�此�System。Exception�此�Exception in action 2 has been caught。 



                                       DON¨T REPEAT ERROR MESSAGES 



   When you throw exceptions�察�make sure that you don¨t use the same error message twice。 Imagine the situa

   tion where you deliver a program into production and the error message ^File not found ̄ is generated。 If this  

   text is used in multiple places�察�when the user calls tech support�察�the support staff will have no idea which file  

   was not found。 Instead�察�in the error message�察�tell the user which file was not found and why。 The more details  

   you deliver�察�the easier it is for the support desk to help users get around the problem。 

         If you do need to use the same text in multiple places�察�add a context identifier。 For example�察�you could  

   generate a load error fault in the context of loading a file using a dialog box。 Or you could generate a load error in  

   the context of loading a file based on a mand´line argument。 Specify each context as an additional piece  

   of information appended or prefixed to the exception text�察�much like the previous code illustrated when catching an  

   exception in action 2。 



Safeguarding Against Stack Unwinding 



Exception handling makes it simple for you to stop your program from crashing�察�but it does not  

help you from ensuring that the state of your application is still intact。 Consider the example  

shown in Figure 5´5�察�which illustrates how program state can be corrupted due to an exception  

that is caught and swallowed。 



Class MyType 

    Public DataMember As Integer 

End Class 

Class Tests                                     LocalDataMember is assigned 



    Public LocalDataMember As Integer 

    Public Sub GeneratesException�┌� 

        Me。LocalDataMember = 10                         DataMember should also be assigned�察�

        Dim cls As MyType = Nothing                       but an exception is raised�察�and the 

        cls。DataMember = 10 

    End Sub                                                  assignment doesn¨t happen 

    Public Sub RunAll�┌� 

        Console。WriteLine�─�LocalDataMember=; & LocalDataMember�� 

        Try                                                                      At this point�察�neither 

            GeneratesException�┌�                                                  member is assigned 

        Catch exception1 As Exception 

        End Try 

        Console。WriteLine�─�LocalDataMember=; & LocalDataMember��                     At this point�察�DataMember is not 

    End Sub                                                                        assigned�察�but LocalDataMember is 

End Class 

                                                                                   assigned�察�indicating a corrupt state 



Figure 5´5。 Exceptions can corrupt the state of a program。 



      When an exception is caught�察�the stack is unwound。 Consider the example shown in  

Figure 5´6�察�where the unwinding of the stack can have the side effect of jumping over a method call。 


´´´´´´´´´´´´´´´´´´´´´´Page 146´´´´´´´´´´´´´´´´´´´´´´´

124       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 



           Figure 5´6。  Unwinding the stack can cause a method call to be skipped。 



                In the example in Figure 5´6�察�the methods are called sequentially�察�starting with RunAll�┌��察 �

           and then when a single exception is thrown�察�the code in the RunAll�┌� method¨s Catch block is  

           executed immediately。 So�察�depth has a value of  2 instead of a value of 0 ��the value you would  

           expect if no exception were thrown�� when execution finishes。 You can see that the stack has  

          unwound too fast�察�leaving the program with unpredictable results。 

                The stack unwinding problem causes corruption and can wreak havoc on your program。  

          A seemingly working program might corrupt itself and slowly fail or generate incorrect data。  

           Fortunately�察�there are a couple ways to handle stack unwinding correctly。 



           Using Finally to Process Unfinished Tasks 



          The simplest solution to the stack unwinding problem is to use the  Finally keyword�察�which  

           indicates that you want to guarantee a piece of code executes�察�whether or not an exception is  

           thrown。 The following shows how you could rewrite the example in Figure 5´6 using  Finally。  

          When executed�察�this code will set the  depth data member to the correct value。 



           Class CallingExample  

              Dim depth As Integer 

              Public Sub CalledCalledMethod�┌�  

                  Me。depth = 2 

                  Throw New Exception�┌� 

              End Sub 


´´´´´´´´´´´´´´´´´´´´´´Page 147´´´´´´´´´´´´´´´´´´´´´´´

                              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 125 



    Public Sub CalledMethod�┌�  

        Me。depth = 1 

        Try  
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��