友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

VB2008从入门到精通(PDF格式英文版)-第60章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




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 Exception 

    Throw N
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!