ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ24²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡application¡¡mode¡ªthe¡¡state¡¡it¡¡is¡¡in¡¡before¡¡it¡¡reaches¡¡the¡¡breakpoint¡¡and¡¡¡¡
after¡¡it¡¡leaves¡¡the¡¡breakpoint¡£¡¡To¡¡leave¡¡debug¡¡mode£»¡¡you¡¡can¡¡press¡¡the¡¡F5¡¡key¡¡to¡¡switch¡¡to¡¡appli
cation¡¡mode¡¡and¡¡continue¡¡executing¡¡the¡¡application£»¡¡or¡¡press¡¡Ctrl£«Alt£«Break¡¡to¡¡stop¡¡debugging¡¡¡¡
and¡¡stop¡¡executing¡¡the¡¡application¡£¡¡
Figure¡¡5¡1¡£¡¡Debugging¡¡an¡¡application¡¡with¡¡a¡¡breakpoint¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Running¡¡an¡¡application¡¡in¡¡the¡¡debugger¡¡is¡¡useful¡¡when¡¡tracking¡¡down¡¡exceptions£»¡¡as¡¡you¡¯ll¡¡¡¡
see¡¡in¡¡the¡¡next¡¡section¡£¡¡
Handling¡¡Exceptions¡¡
Those¡¡who¡¡recall¡¡the¡¡¡°good¡¡old¡¡days¡±¡¡of¡¡Windows¡¡3¡£0¡¡and¡¡its¡¡16¡¡bits¡¡will¡¡also¡¡remember¡¡the¡¡¡¡
dreaded¡¡three¡finger¡¡salute¡£¡¡The¡¡three¡finger¡¡salute¡¡refers¡¡to¡¡pressing¡¡Ctrl£«Alt£«Delete¡¡to¡¡reboot¡¡¡¡
Windows¡¡after¡¡a¡¡program¡¡crashed¡£¡¡You¡¡did¡¡not¡¡have¡¡the¡¡chance¡¡to¡¡save¡¡your¡¡work£»¡¡you¡¡just¡¡had¡¡¡¡
to¡¡watch¡¡everything¡¡go¡¡down¡¡the¡¡drain¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡missed¡¡those¡¡puting¡¡days£»¡¡count¡¡yourself¡¡lucky¡£¡¡Nowadays£»¡¡we¡¡have¡¡mechanisms¡¡to¡¡¡¡
catch¡¡unexpected¡¡errors¡¡and¡¡make¡¡sure¡¡that¡¡the¡¡program¡¡or¡¡operating¡¡system¡¡keeps¡¡on¡¡processing¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡140¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
118¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡big¡¡deal¡¡with¡¡modern¡¡operating¡¡systems¡¡and¡¡programming¡¡environments¡¡like¡¡the¡¡CLR¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡they¡¡can¡¡stop¡¡a¡¡single¡¡task¡¡from¡¡disrupting¡¡the¡¡operation¡¡of¡¡the¡¡CPU¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Catching¡¡Exceptions¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Recall¡¡that¡¡in¡¡Chapter¡¡2£»¡¡Visual¡¡Basic¡¡Express¡¡interrupted¡¡the¡¡flow¡¡of¡¡the¡¡program¡¡by¡¡catching¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡exception¡¡generated¡¡by¡¡a¡¡mathematical¡¡overflow¡¡situation¡£¡¡This¡¡is¡¡like¡¡your¡¡driving¡¡teacher¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡realizing¡¡you¡¡are¡¡making¡¡a¡¡mistake¡¡and¡¡pressing¡¡hard¡¡on¡¡the¡¡brake£»¡¡so¡¡you¡¡don¡¯t¡¡hit¡¡a¡¡tree£»¡¡person£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡or¡¡another¡¡car¡£¡¡So£»¡¡in¡¡a¡¡sense£»¡¡you¡¡can¡¡think¡¡of¡¡the¡¡CLR¡¡exception¡handling¡¡mechanism¡¡as¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡teacher¡¡stepping¡¡on¡¡the¡¡brake¡¡when¡¡something¡¡devastating¡¡is¡¡about¡¡to¡¡happen¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Stepping¡¡on¡¡the¡¡brake¡¡stops¡¡the¡¡devastation£»¡¡but¡¡you£»¡¡as¡¡the¡¡driver£»¡¡will¡¡get¡¡a¡¡shock¡¡because¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡error¡¡that¡¡you¡¡were¡¡about¡¡to¡¡make¡£¡¡Likewise£»¡¡when¡¡an¡¡exception¡¡is¡¡triggered£»¡¡the¡¡program¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡gets¡¡a¡¡shock¡£¡¡How¡¡you¡¡deal¡¡with¡¡the¡¡shock¡¡determines¡¡the¡¡fate¡¡of¡¡the¡¡program¡£¡¡In¡¡the¡¡example¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡overflow¡¡error¡¡in¡¡Chapter¡¡2£»¡¡the¡¡shock¡¡was¡¡captured¡¡by¡¡the¡¡IDE£»¡¡and¡¡thus¡¡a¡¡friendly£»¡¡easy
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡understand¡¡user¡¡interface¡¡was¡¡presented¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Consider¡¡the¡¡source¡¡code¡¡shown¡¡in¡¡Figure¡¡5¡2£»¡¡which¡¡generates¡¡an¡¡exception¡£¡¡This¡¡is¡¡referred¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡as¡¡¡¡throwing¡¡an¡¡exception¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡declaration¡¡with¡¡a¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡single¡¡data¡¡member¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡is¡¡declared£»¡¡but¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡MyType¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡DataMember¡¡As¡¡Integer¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡instantiated¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡Tests¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡GeneratesException£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡cls¡¡As¡¡MyType¡¡=¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cls¡£DataMember¡¡=¡¡10¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡RunAll£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Data¡¡member¡¡of¡¡the¡¡class¡¡is¡¡assigned¡¡even¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡GeneratesException£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡though¡¡the¡¡class¡¡has¡¡not¡¡been¡¡instantiated¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡will¡¡cause¡¡an¡¡exception¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡5¡2¡£¡¡¡¡Throwing¡¡an¡¡exception¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡the¡¡RunAll£¨£©¡¡method¡¡were¡¡executed£»¡¡the¡¡exception¡¡shown¡¡in¡¡Figure¡¡5¡3¡¡would¡¡be¡¡generated¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡Visual¡¡Basic¡¡Express¡¡£¨use¡¡F5¡¡to¡¡run¡¡the¡¡debugger£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡exception¡¡did¡¡not¡¡cause¡¡the¡¡operating¡¡system¡¡or¡¡Visual¡¡Basic¡¡Express¡¡to¡¡crash£»¡¡because¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Visual¡¡Studio¡¯s¡¡built¡in¡¡exception¡¡handler¡¡caught¡¡it¡£¡¡Visual¡¡Studio¡¡hit¡¡the¡¡brake¡¡and¡¡made¡¡sure¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡only¡¡your¡¡program¡¡stopped¡¡working¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imagine¡¡if¡¡Visual¡¡Studio¡¡were¡¡not¡¡running¡£¡¡The¡¡generated¡¡exception¡¡would¡¡cause¡¡the¡¡program¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡stop¡¡in¡¡its¡¡tracks£»¡¡and¡¡a¡¡messy¡¡error¡¡message¡¡would¡¡appear£»¡¡referencing¡¡objects£»¡¡line¡¡numbers£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡the¡¡stack¡£¡¡Most¡¡users¡¡would¡¡have¡¡no¡¡idea¡¡what¡¡happened£»¡¡and¡¡be¡¡left¡¡with¡¡a¡¡program¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡was¡¡not¡¡working¡¡anymore¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡141¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡119¡¡
Figure¡¡5¡3¡£¡¡An¡¡exception¡¡generated¡¡by¡¡a¡¡null¡¡reference¡¡
¡¡¡¡¡¡¡¡¡¡What¡¡you¡¡want¡¡to¡¡do¡¡is¡¡catch¡¡the¡¡exception¡¡as¡¡Visual¡¡Studio¡¡did¡£¡¡For¡¡example£»¡¡if¡¡you¡¡knew¡¡¡¡
that¡¡an¡¡exception¡¡could¡¡occur¡¡in¡¡RunAll£¨£©£»¡¡you¡¡could¡¡modify¡¡the¡¡code¡¡as¡¡follows£º¡¡
Class¡¡MyType¡¡¡¡
¡¡¡¡¡¡¡¡Public¡¡DataMember¡¡As¡¡Integer¡¡
End¡¡Class¡¡
Class¡¡Tests¡¡¡¡
¡¡¡¡¡¡¡¡Public¡¡Sub¡¡GeneratesException£¨£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡cls¡¡As¡¡MyType¡¡=¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cls¡£DataMember¡¡=¡¡10¡¡
¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡Public¡¡Sub¡¡RunAll£¨£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Try¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡GeneratesException£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Catch¡¡exception1¡¡As¡¡Exception¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Try¡¡
¡¡¡¡¡¡¡¡End¡¡Sub¡¡
End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡code¡¡is¡¡an¡¡exception¡¡block£»¡¡which¡¡catches¡¡an¡¡exception¡¡and¡¡allows¡¡you¡¡to¡¡respond¡¡¡¡
to¡¡it¡£¡¡In¡¡this¡¡example£»¡¡nothing¡¡happens¡¡after¡¡catching¡¡the¡¡exception¡£¡¡If¡¡you¡¡run¡¡this¡¡program£»¡¡¡¡
Visual¡¡Basic¡¡will¡¡not¡¡generate¡¡an¡¡exception¡¡warning£»¡¡and¡¡the¡¡program¡¡will¡¡run¡¡without¡¡any¡¡¡¡
problems¡£¡¡From¡¡the¡¡perspective¡¡of¡¡Visual¡¡Basic£»¡¡everything¡¡worked¡¡and¡¡is¡¡running¡¡OK¡£¡¡
¡¡¡¡¡¡¡¡¡¡But¡¡if¡¡you¡¡stop¡¡and¡¡think¡¡about¡¡it£»¡¡is¡¡everything¡¡really¡¡OK£¿¡¡Even¡¡though¡¡the¡¡program¡¡¡¡
continued¡¡to¡¡execute£»¡¡was¡¡the¡¡program¡¡logically¡¡correct£¿¡¡The¡¡answer¡¡is¡¡no£»¡¡because¡¡what¡¡the¡¡¡¡
program¡¡did¡¡was¡¡swallow¡¡an¡¡exception¡¡without¡¡doing¡¡anything¡¡to¡¡remedy¡¡the¡¡problem¡£¡¡You¡¡¡¡
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¡¡New¡¡Exception£¨¡¨Exception¡¡in¡¡action¡¡2¡¡has¡¡been¡¡caught¡£¡¨£»¡¡thr
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡