Сţµç×ÓÊé > ÆäËûµç×ÓÊé > VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) >

µÚ46ÕÂ

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ46ÕÂ

С˵£º VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) ×ÖÊý£º ÿҳ3500×Ö

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 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¡¡¡¡

·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨2£© ²È£¨2£©

Äã¿ÉÄÜϲ»¶µÄ