ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
¸»Ê¿¿µÐ¡ËµÍø ·µ»Ø±¾ÊéĿ¼ ¼ÓÈëÊéÇ© ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ¡ºÊղص½ÎÒµÄä¯ÀÀÆ÷¡»

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ25²¿·Ö

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CalledCalledMethod£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Finally¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£depth¡¡=¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Try¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡

¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Method£¨£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£depth¡¡=¡¡0¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Try¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CalledMethod£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Finally¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£depth¡¡=¡¡0¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Try¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡

¡¡¡¡¡¡¡¡Public¡¡Function¡¡GetDepth£¨£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Me¡£depth¡¡

¡¡¡¡¡¡¡¡End¡¡Function¡¡

End¡¡Class¡¡



Class¡¡Tests¡¡¡¡

¡¡¡¡¡¡¡¡Sub¡¡TestCallingExample£¨£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡cls¡¡As¡¡CallingExample¡¡=¡¡Nothing¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Try¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cls¡¡=¡¡new¡¡CallingExample£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cls¡£Method£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Catch¡¡¡¡exception1¡¡As¡¡Exception¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Try¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Depth¡¡is¡¡£¨¡¨¡¡&¡¡cls¡£GetDepth£¨£©¡¡&¡¡¡¨£©¡¨£©¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡

¡¡¡¡¡¡¡¡Public¡¡Sub¡¡RunAll£¨£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestCallingExample£¨£©¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡example£»¡¡each¡¡¡¡Finally¡¡keyword¡¡is¡¡associated¡¡with¡¡a¡¡Try¡¡block¡£¡¡You¡¡don¡¯t¡¡need¡¡to¡¡¡¡

associate¡¡a¡¡Catch¡¡block¡¡with¡¡a¡¡Try¡¡block¡¡if¡¡you¡¡are¡¡using¡¡¡¡Finally¡£¡¡If¡¡the¡¡Try¡¡block¡¡is¡¡entered£»¡¡¡¡

when¡¡the¡¡code¡¡leaves¡¡the¡¡Try¡¡block£»¡¡regardless¡¡of¡¡whether¡¡it¡¡ran¡¡successfully¡¡or¡¡threw¡¡an¡¡excep

tion£»¡¡the¡¡code¡¡within¡¡Finally¡¡is¡¡executed¡£¡¡Thus£»¡¡if¡¡stack¡¡unwinding¡¡occurs£»¡¡it¡¡is¡¡possible¡¡to¡¡reset¡¡¡¡

or¡¡assign¡¡state¡¡to¡¡a¡¡consistent¡¡value¡¡before¡¡the¡¡exception¡¡is¡¡processed¡¡elsewhere¡£¡¡



¡öNote¡¡¡¡When¡¡the¡¡¡¡Finally¡¡block¡¡is¡¡called£»¡¡you¡¡don¡¯t¡¡know¡¡if¡¡it¡¡is¡¡called¡¡due¡¡to¡¡an¡¡exception¡¡or¡¡successful¡¡¡¡

code¡¡execution¡£¡¡Thus£»¡¡you¡¡shouldn¡¯t¡¡assume¡¡that¡¡Finally¡¡is¡¡called¡¡because¡¡an¡¡exception¡¡was¡¡thrown¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡148¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

126¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Sandboxing¡¡Your¡¡Code¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡sandboxing¡¡technique¡¡is¡¡similar¡¡to¡¡playing¡¡with¡¡an¡¡Etch¡¡A¡¡Sketch£»¡¡in¡¡that¡¡you¡¡attempt¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡build¡¡a¡¡state£»¡¡and¡¡if¡¡you¡¡make¡¡a¡¡mistake£»¡¡you¡¡can¡¡throw¡¡the¡¡state¡¡away¡£¡¡This¡¡requires¡¡that¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡separate¡¡your¡¡code¡¡into¡¡three¡¡distinct¡¡steps£º¡¡declaration£»¡¡manipulation£»¡¡and¡¡integration¡£¡¡Figure¡¡5¡­7¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡illustrates¡¡the¡¡three¡¡steps¡¡in¡¡sandboxing¡¡your¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡Declaration£º¡¡Each¡¡variable¡¡is¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡declared¡¡and¡¡assigned¡¡a¡¡default¡¡state¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Function¡¡Separated£¨£©¡¡As¡¡SeparateSteps¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡will¡¡not¡¡generate¡¡an¡¡exception¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡cls¡¡As¡¡SeparateSteps¡¡=¡¡Nothing¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cls¡¡=¡¡New¡¡SeparateSteps£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡Manipulation£º¡¡Each¡¡variable¡¡is¡¡manipulated¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cls¡£PossibleExceptionMethod£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡methods¡¡to¡¡create¡¡a¡¡state¡¡that¡¡does¡¡not¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡cls¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡alter¡¡anything¡¡outside¡¡the¡¡method¡£¡¡The¡¡variables¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡may¡¡reference¡¡the¡¡global¡¡state£»¡¡but¡¡not¡¡change¡¡it¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡Integration£º¡¡Once¡¡the¡¡state¡¡has¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡been¡¡modified£»¡¡an¡¡integration¡¡into¡¡the¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bigger¡¡system¡¡can¡¡occur¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡5¡­7¡£¡¡Sandboxing¡¡your¡¡code¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡5¡­7¡¡illustrates¡¡one¡¡way¡¡of¡¡performing¡¡a¡¡sandboxing¡¡operation£»¡¡but¡¡there¡¡are¡¡many¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡other¡¡possible¡¡implementations¡£¡¡In¡¡each¡¡of¡¡the¡¡different¡¡implementations£»¡¡the¡¡objective¡¡is¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡same£º¡¡you¡¡want¡¡to¡¡perform¡¡the¡¡operations¡¡that¡¡could¡¡cause¡¡an¡¡exception¡¡apart¡¡from¡¡the¡¡main¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡code¡£¡¡Then¡¡if¡¡you¡¡do¡¡have¡¡an¡¡exception£»¡¡it¡¡will¡¡be¡¡localized¡¡to¡¡the¡¡separate¡¡code¡£¡¡When¡¡the¡¡stack¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡unwinds£»¡¡the¡¡other¡¡code¡¡won¡¯t¡¡be¡¡corrupted¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡rule¡¡of¡¡thumb¡¡with¡¡sandboxing¡¡your¡¡code¡¡is¡¡to¡¡keep¡¡all¡¡of¡¡the¡¡code¡¡manipulations¡¡that¡¡could¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generate¡¡an¡¡exception¡¡separate¡¡from¡¡any¡¡existing¡¡state¡¡that¡¡could¡¡be¡¡corrupted¡£¡¡Once¡¡the¡¡manipulations¡¡have¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡been¡¡carried¡¡out£»¡¡you¡¡integrate¡¡the¡¡objects¡¡into¡¡the¡¡global¡¡state¡¡using¡¡method¡¡calls¡¡that¡¡are¡¡extremely¡¡unlikely¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡generate¡¡exceptions¡£¡¡For¡¡those¡¡situations¡¡when¡¡you¡¡must¡¡manipulate¡¡existing¡¡state£»¡¡use¡¡a¡¡¡¡Finally¡¡handler£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡so¡¡that¡¡the¡¡previous¡¡existing¡¡state¡¡can¡¡be¡¡re¡­created¡¡if¡¡necessary¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Filtering¡¡Exceptions¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡all¡¡of¡¡the¡¡examples¡¡presented¡¡so¡¡far¡¡in¡¡this¡¡chapter£»¡¡the¡¡Catch¡¡statement¡¡used¡¡the¡¡Exception¡¡type£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Catch¡¡exception1¡¡As¡¡Exception¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Exception¡¡will¡¡catch¡¡every¡¡exception¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡149¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡127¡¡



¡¡¡¡¡¡¡¡¡¡In¡¡Figure¡¡5¡­3£»¡¡the¡¡IDE¡¡caught¡¡an¡¡exception£»¡¡referencing¡¡NullReferenceException£»¡¡which¡¡is¡¡¡¡

a¡¡specific¡¡type¡¡of¡¡exception¡£¡¡When¡¡you¡¡use¡¡it¡¡in¡¡a¡¡Catch¡¡statement£»¡¡you¡¡will¡¡catch¡¡only¡¡null¡¡refer

ence¡¡exceptions¡£¡¡

¡¡¡¡¡¡¡¡¡¡By¡¡specializing¡¡the¡¡exception£»¡¡you¡¡have¡¡the¡¡ability¡¡to¡¡filter¡¡which¡¡exception¡¡you¡¡want¡¡to¡¡¡¡

catch¡£¡¡For¡¡example£»¡¡NotSupportedException¡¡will¡¡catch¡¡only¡¡instances¡¡of¡¡NotSupportedException¡¡¡¡

exceptions¡£¡¡Here¡¡is¡¡an¡¡example£º¡¡



Try¡¡¡¡

¡¡¡¡¡¡¡¡Throw¡¡New¡¡NotSupportedException£¨¡¨There¡¡is¡¡no¡¡code¡¨£©¡¡

Catch¡¡ex¡¡As¡¡NotSupportedException¡¡

End¡¡Try¡¡



¡¡¡¡¡¡¡¡¡¡If¡¡the¡¡code¡¡within¡¡the¡¡Try¡¡block¡¡threw¡¡an¡¡instance¡¡of¡¡Exception£»¡¡the¡¡Catch¡¡block¡¡would¡¡not¡¡¡¡

trigger£»¡¡because¡¡the¡¡Catch¡¡block¡¡was¡¡looking¡¡for¡¡a¡¡specific¡¡exception¡¡type¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Exception¡¡types¡¡can¡¡be¡¡bined¡¡to¡¡provide¡¡specific¡¡filtering¡¡capabilities¡£¡¡The¡¡specific¡¡¡¡

exception¡¡type¡¡should¡¡be¡¡the¡¡first¡¡exception¡¡after¡¡the¡¡Try¡¡block£»¡¡as¡¡in¡¡this¡¡example£º¡¡



Try¡¡¡¡

¡¡¡¡¡¡¡¡'¡¡¡£¡¡¡£¡¡¡£¡¡

Catch¡¡ex1¡¡As¡¡NotSupportedException¡¡

¡¡¡¡¡¡¡¡¡¡'¡¡

Catch¡¡ex2¡¡As¡¡Exception¡¡

¡¡¡¡¡¡¡¡¡¡'¡¡

End¡¡Try¡¡



¡¡¡¡¡¡¡¡¡¡By¡¡bining¡¡multiple¡¡exception¡¡filters£»¡¡you¡¡don¡¯t¡¡need¡¡to¡¡figure¡¡out¡¡which¡¡kind¡¡of¡¡excep

tion¡¡is¡¡being¡¡thrown¡£¡¡For¡¡example£»¡¡without¡¡the¡¡filtering¡¡capabilities¡¡of¡¡a¡¡Catch¡¡block¡¡to¡¡catch¡¡¡¡

NotSupportedException¡¡exception¡¡types£»¡¡you¡¡would¡¡need¡¡to¡¡write¡¡this¡¡code£º¡¡



Try¡¡¡¡

¡¡¡¡¡¡¡¡Throw¡¡New¡¡NotSupportedException£¨¡¨There¡¡is¡¡no¡¡code¡¨£©¡¡

Catch¡¡ex¡¡As¡¡Exception¡¡

¡¡¡¡¡¡¡¡If¡¡ex¡¡Is¡¡NotSupportedException¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡'¡£¡¡¡£¡¡¡£¡¡

¡¡¡¡¡¡¡¡Else¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw¡¡ex¡¡

¡¡¡¡¡¡¡¡End¡¡If¡¡

End¡¡Try¡¡



¡¡¡¡¡¡¡¡¡¡Table¡¡5¡­1¡¡lists¡¡mon¡¡exception¡¡types¡¡in¡¡the¡¡System¡¡namespace¡¡that¡¡you¡¡can¡¡throw¡¡or¡¡¡¡

can¡¡be¡¡thrown¡£¡¡There¡¡are¡¡many¡¡more¡¡exceptions£»¡¡and¡¡you¡¡can¡¡even¡¡generate¡¡your¡¡own¡¡excep

tions¡¡by¡¡subclassing¡¡¡¡Exception¡£¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡150¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

128¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Table¡¡5¡­1¡£¡¡¡¡mon¡¡Exception¡¡Types¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Exception¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Description¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Exception¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Plain¡­vanilla¡¡exception£»¡¡a¡¡general¡¡container¡¡for¡¡all¡¡exc
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨11£© ²È£¨11£©
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡