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

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

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




as “File XYZ is not present and thus I cannot continue。” Upon seeing such an error message;  

another developer would know that he should check whether the file is actually there。 

     This chapter explains exceptions; as errors in an application are technically known; and how  

to handle them。 We’ll begin with an overview of how exceptions fit into the structure of a program。  



Understanding Errors; Exceptions; and  

Exception Handling 



An error is when something is not right and could be the result of incorrect data or an incorrect  

calculation。 However; the  CLR does not understand errors; it understands only exceptions。 

     For example; if an error is caused by multiplying two numbers rather than adding them;  

your program will continue to function; but will produce the wrong results。 A similar error occurs  

when a user enters the wrong data—the answer will be wrong; but the program will still run。 

     In the case of a serious problem that is beyond the control of the user or that threatens to crash  

your program; the CLR steps in and treats this as an exception。 This interrupts the program and  

allows your code to deal with the problem; rather than letting the program crash。 (Some would  

argue that an exception will not halt your entire program; but a thread of your program; while  



                                                                                                              115 


…………………………………………………………Page 138……………………………………………………………

116       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 



           this is technically true; it’s not an important distinction for this introduction to exceptions。)  

           This is called  exception handling。 

                To understand how code organization affects exception handling; think of an application  

           as a large corporation。 A corporation has a chief executive officer (CEO); then first…level managers;  

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