±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚcnblogs,ÎÄÕÂÖ÷Òª´Ó»ù´¡¸ÅÄî˵Æð£¬ÓÖ½²½âÁËÌåϵ½á¹¹£¬JavaµÄÒì³£´¦Àí»úÖÆ£¬Òì³£Á´µÈÏà¹Ø£¬ÏêϸÄÚÈÝÇë¿´ÏÂÎÄ¡£
|
|
Òì³£µÄ¸ÅÄî Òì³£ÊdzÌÐòÖеÄһЩ´íÎ󣬵«²¢²»ÊÇËùÓеĴíÎó¶¼ÊÇÒì³££¬²¢ÇÒ´íÎóÓÐʱºòÊÇ¿ÉÒÔ±ÜÃâµÄ¡£
±ÈÈç˵£¬ÄãµÄ´úÂëÉÙÁËÒ»¸ö·ÖºÅ£¬ÄÇôÔËÐгöÀ´½á¹ûÊÇÌáʾÊÇ´íÎójava.lang.Error£»Èç¹ûÄãÓÃSystem.out.println(11/0)£¬ÄÇôÄãÊÇÒòΪÄãÓÃ0×öÁ˳ýÊý£¬»áÅ׳öjava.lang.ArithmeticExceptionµÄÒì³£¡£
Òì³£·¢ÉúµÄÔÒòÓкܶ࣬ͨ³£°üº¬ÒÔϼ¸´óÀࣺ
Óû§ÊäÈëÁË·Ç·¨Êý¾Ý¡£ Òª´ò¿ªµÄÎļþ²»´æÔÚ¡£ ÍøÂçͨÐÅʱÁ¬½ÓÖжϣ¬»òÕßJVMÄÚ´æÒç³ö¡£ ÕâЩÒì³£ÓеÄÊÇÒòΪÓû§´íÎóÒýÆð£¬ÓеÄÊdzÌÐò´íÎóÒýÆðµÄ£¬»¹ÓÐÆäËüһЩÊÇÒòΪÎïÀí´íÎóÒýÆðµÄ¡£-
ÒªÀí½âJavaÒì³£´¦ÀíÊÇÈçºÎ¹¤×÷µÄ£¬ÄãÐèÒªÕÆÎÕÒÔÏÂÈýÖÖÀàÐ͵ÄÒì³££º
¼ì²éÐÔÒì³££º×î¾ß´ú±íµÄ¼ì²éÐÔÒì³£ÊÇÓû§´íÎó»òÎÊÌâÒýÆðµÄÒì³££¬ÕâÊdzÌÐòÔ±ÎÞ·¨Ô¤¼ûµÄ¡£ÀýÈçÒª´ò¿ªÒ»¸ö²»´æÔÚÎļþʱ£¬Ò»¸öÒì³£¾Í·¢ÉúÁË£¬ÕâЩÒì³£ÔÚ±àÒëʱ²»Äܱ»¼òµ¥µØºöÂÔ¡£ ÔËÐÐʱÒì³££º ÔËÐÐʱÒì³£ÊÇ¿ÉÄܱ»³ÌÐòÔ±±ÜÃâµÄÒì³£¡£Óë¼ì²éÐÔÒì³£Ïà·´£¬ÔËÐÐʱÒì³£¿ÉÒÔÔÚ±àÒëʱ±»ºöÂÔ¡£ ´íÎó£º ´íÎó²»ÊÇÒì³££¬¶øÊÇÍÑÀë³ÌÐòÔ±¿ØÖƵÄÎÊÌâ¡£´íÎóÔÚ´úÂëÖÐͨ³£±»ºöÂÔ¡£ÀýÈ磬µ±Õ»Òç³öʱ£¬Ò»¸ö´íÎó¾Í·¢ÉúÁË£¬ËüÃÇÔÚ±àÒëÒ²¼ì²é²»µ½µÄ¡£
Òì³£Ö¸²»ÆÚ¶øÖÁµÄ¸÷ÖÖ×´¿ö£¬È磺ÎļþÕÒ²»µ½¡¢ÍøÂçÁ¬½Óʧ°Ü¡¢³ý0²Ù×÷¡¢·Ç·¨²ÎÊýµÈ¡£Òì³£ÊÇÒ»¸öʼþ£¬Ëü·¢ÉúÔÚ³ÌÐòÔËÐÐÆÚ¼ä£¬¸ÉÈÅÁËÕý³£µÄÖ¸ÁîÁ÷³Ì¡£
JavaÓïÑÔÔÚÉè¼ÆµÄµ±³õ¾Í¿¼Âǵ½ÕâЩÎÊÌ⣬Ìá³öÒì³£´¦ÀíµÄ¿ò¼ÜµÄ·½°¸£¬ËùÓеÄÒì³£¶¼¿ÉÒÔÓÃÒ»¸öÒì³£ÀàÀ´±íʾ£¬²»Í¬ÀàÐ͵ÄÒì³£¶ÔÓ¦²»Í¬µÄ×ÓÀàÒì³££¨Ä¿Ç°ÎÒÃÇËù˵µÄÒì³£°üÀ¨´íÎó¸ÅÄ£¬¶¨ÒåÒì³£´¦ÀíµÄ¹æ·¶£¬ÔÚJDK1.4°æ±¾ÒÔºóÔö¼ÓÁËÒì³£Á´»úÖÆ£¬´Ó¶ø±ãÓÚ¸ú×ÙÒì³£¡£
JavaÒì³£ÊÇÒ»¸öÃèÊöÔÚ´úÂë¶ÎÖз¢ÉúÒì³£µÄ¶ÔÏ󣬵±·¢ÉúÒì³£Çé¿öʱ£¬Ò»¸ö´ú±í¸ÃÒì³£µÄ¶ÔÏó±»´´½¨²¢ÇÒÔÚµ¼Ö¸ÃÒì³£µÄ·½·¨Öб»Å׳ö£¬¶ø¸Ã·½·¨¿ÉÒÔÑ¡Ôñ×Ô¼º´¦ÀíÒì³£»òÕß´«µÝ¸ÃÒì³£¡£
Òì³£µÄÌåϵ½á¹¹ Java°ÑÒì³£µ±×÷¶ÔÏóÀ´´¦Àí£¬²¢¶¨ÒåÒ»¸ö»ùÀàjava.lang.Throwable×÷ΪËùÓÐÒì³£µÄ³¬Àà¡£
ÔÚJava APIÖÐÒѾ¶¨ÒåÁËÐí¶àÒì³£À࣬ÕâЩÒì³£Àà·ÖΪÁ½´óÀ࣬´íÎóErrorºÍÒì³£Exception¡£
JavaÒì³£²ã´Î½á¹¹Í¼ÈçÏÂͼËùʾ£º

´ÓͼÖпÉÒÔ¿´³öËùÓÐÒì³£ÀàÐͶ¼ÊÇÄÚÖÃÀàThrowableµÄ×ÓÀ࣬Òò¶øThrowableÔÚÒì³£ÀàµÄ²ã´Î½á¹¹µÄ¶¥²ã¡£
½ÓÏÂÀ´Throwable·Ö³ÉÁËÁ½¸ö²»Í¬µÄ·ÖÖ§£¬Ò»¸ö·ÖÖ§ÊÇError£¬Ëü±íʾ²»Ï£Íû±»³ÌÐò²¶»ñ»òÕßÊdzÌÐòÎÞ·¨´¦ÀíµÄ´íÎó¡£ÁíÒ»¸ö·ÖÖ§ÊÇException£¬Ëü±íʾÓû§³ÌÐò¿ÉÄܲ¶×½µÄÒì³£Çé¿ö»òÕß˵ÊdzÌÐò¿ÉÒÔ´¦ÀíµÄÒì³£¡£ÆäÖÐÒì³£ÀàExceptionÓÖ·ÖΪÔËÐÐʱÒì³£(RuntimeException)ºÍ·ÇÔËÐÐʱÒì³£¡£
JavaÒì³£ÓÖ¿ÉÒÔ·ÖΪ²»Êܼì²éÒì³££¨Unchecked Exception£©ºÍ¼ì²éÒì³££¨Checked
Exception£©¡£
ÏÂÃæ½«Ïêϸ½²ÊöÕâЩÒì³£Ö®¼äµÄÇø±ðÓëÁªÏµ£º
Error£ºErrorÀà¶ÔÏóÓÉ Java ÐéÄâ»úÉú³É²¢Å׳ö£¬´ó¶àÊý´íÎóÓë´úÂë±àдÕßËùÖ´ÐеIJÙ×÷Î޹ء£ÀýÈ磬JavaÐéÄâ»úÔËÐдíÎó£¨Virtual
MachineError£©£¬µ±JVM²»ÔÙÓмÌÐøÖ´ÐвÙ×÷ËùÐèµÄÄÚ´æ×ÊԴʱ£¬½«³öÏÖ OutOfMemoryError¡£ÕâЩÒì³£·¢Éúʱ£¬JavaÐéÄâ»ú£¨JVM£©Ò»°ã»áÑ¡ÔñÏß³ÌÖÕÖ¹£»»¹Óз¢ÉúÔÚÐéÄâ»úÊÔͼִÐÐÓ¦ÓÃʱ£¬ÈçÀඨÒå´íÎó£¨NoClassDefFoundError£©¡¢Á´½Ó´íÎó£¨LinkageError£©¡£ÕâЩ´íÎóÊDz»¿É²éµÄ£¬ÒòΪËüÃÇÔÚÓ¦ÓóÌÐòµÄ¿ØÖƺʹ¦ÀíÄÜÁ¦Ö®
Í⣬¶øÇÒ¾ø´ó¶àÊýÊdzÌÐòÔËÐÐʱ²»ÔÊÐí³öÏÖµÄ×´¿ö¡£¶ÔÓÚÉè¼ÆºÏÀíµÄÓ¦ÓóÌÐòÀ´Ëµ£¬¼´Ê¹È·Êµ·¢ÉúÁË´íÎ󣬱¾ÖÊÉÏÒ²²»Ó¦¸ÃÊÔͼȥ´¦ÀíËüËùÒýÆðµÄÒì³£×´¿ö¡£ÔÚJavaÖУ¬´íÎóͨ³£ÊÇʹÓÃErrorµÄ×ÓÀàÃèÊö¡£ Exception£ºÔÚException·ÖÖ§ÖÐÓÐÒ»¸öÖØÒªµÄ×ÓÀàRuntimeException£¨ÔËÐÐʱÒì³££©£¬¸ÃÀàÐ͵ÄÒì³£×Ô¶¯ÎªÄãËù±àдµÄ³ÌÐò¶¨ÒåArrayIndexOutOfBoundsException£¨Êý×éϱêÔ½½ç£©¡¢NullPointerException£¨¿ÕÖ¸ÕëÒì³££©¡¢ArithmeticException£¨ËãÊõÒì³££©¡¢MissingResourceException£¨¶ªÊ§×ÊÔ´£©¡¢ClassNotFoundException£¨ÕÒ²»µ½ÀࣩµÈÒì³££¬ÕâЩÒì³£ÊDz»¼ì²éÒì³££¬³ÌÐòÖпÉÒÔÑ¡Ôñ²¶»ñ´¦Àí£¬Ò²¿ÉÒÔ²»´¦Àí¡£ÕâЩÒì³£Ò»°ãÊÇÓɳÌÐòÂß¼´íÎóÒýÆðµÄ£¬³ÌÐòÓ¦¸Ã´ÓÂß¼½Ç¶È¾¡¿ÉÄܱÜÃâÕâÀàÒì³£µÄ·¢Éú£»¶øRuntimeExceptionÖ®ÍâµÄÒì³£ÎÒÃÇͳ³ÆÎª·ÇÔËÐÐʱÒì³££¬ÀàÐÍÉÏÊôÓÚExceptionÀ༰Æä×ÓÀ࣬´Ó³ÌÐòÓï·¨½Ç¶È½²ÊDZØÐë½øÐд¦ÀíµÄÒì³££¬Èç¹û²»´¦Àí£¬³ÌÐò¾Í²»ÄܱàÒëͨ¹ý¡£ÈçIOException¡¢SQLExceptionµÈÒÔ¼°Óû§×Ô¶¨ÒåµÄExceptionÒì³££¬Ò»°ãÇé¿öϲ»×Ô¶¨Òå¼ì²éÒì³£¡£ ×¢Òâ
ErrorºÍExceptionµÄÇø±ð£ºErrorͨ³£ÊÇÔÖÄÑÐÔµÄÖÂÃüµÄ´íÎó£¬ÊdzÌÐòÎÞ·¨¿ØÖƺʹ¦ÀíµÄ£¬µ±³öÏÖÕâЩÒ쳣ʱ£¬JavaÐéÄâ»ú£¨JVM£©Ò»°ã»áÑ¡ÔñÖÕÖ¹Ị̈߳»Exceptionͨ³£Çé¿öÏÂÊÇ¿ÉÒÔ±»³ÌÐò´¦ÀíµÄ£¬²¢ÇÒÔÚ³ÌÐòÖÐÓ¦¸Ã¾¡¿ÉÄܵÄÈ¥´¦ÀíÕâЩÒì³£¡£
¼ì²éÒì³££ºÔÚÕýÈ·µÄ³ÌÐòÔËÐйý³ÌÖУ¬ºÜÈÝÒ׳öÏֵġ¢ÇéÀí¿ÉÈݵÄÒì³£×´¿ö£¬ÔÚÒ»¶¨³Ì¶ÈÉÏÕâÖÖÒì³£µÄ·¢ÉúÊÇ¿ÉÒÔÔ¤²âµÄ£¬²¢ÇÒÒ»µ©·¢Éú¸ÃÖÖÒì³££¬¾Í±ØÐë²ÉȡijÖÖ·½Ê½½øÐд¦Àí¡£
Ìáʾ
³ýÁËRuntimeException¼°Æä×ÓÀàÒÔÍ⣬ÆäËûµÄExceptionÀ༰Æä×ÓÀà¶¼ÊôÓÚ¼ì²éÒì³££¬µ±³ÌÐòÖпÉÄܳöÏÖÕâÀàÒì³££¬ÒªÃ´Ê¹ÓÃtry-catchÓï¾ä½øÐв¶»ñ£¬ÒªÃ´ÓÃthrows×Ó¾äÅ׳ö£¬·ñÔò±àÒëÎÞ·¨Í¨¹ý¡£
²»Êܼì²éÒì³££º°üÀ¨RuntimeException¼°Æä×ÓÀàºÍError¡£
Ìáʾ
²»Êܼì²éÒ쳣Ϊ±àÒëÆ÷²»ÒªÇóÇ¿ÖÆ´¦ÀíµÄÒì³££¬¼ì²éÒì³£ÔòÊDZàÒëÆ÷ÒªÇó±ØÐë´¦ÖõÄÒì³£¡£
Java Òì³£µÄ´¦Àí»úÖÆ JavaµÄÒì³£´¦Àí±¾ÖÊÉÏÊÇÅ׳öÒì³£ºÍ²¶»ñÒì³£¡£
Å׳öÒì³££ºÒªÀí½âÅ׳öÒì³££¬Ê×ÏÈÒªÃ÷°×ʲôÊÇÒì³£ÇéÐΣ¨exception condition£©£¬ËüÊÇÖ¸×èÖ¹µ±Ç°·½·¨»ò×÷ÓÃÓò¼ÌÐøÖ´ÐеÄÎÊÌâ¡£Æä´Î°ÑÒì³£ÇéÐÎºÍÆÕͨÎÊÌâÏàÇø·Ö£¬ÆÕͨÎÊÌâÊÇÖ¸ÔÚµ±Ç°»·¾³ÏÂÄܵõ½×ã¹»µÄÐÅÏ¢£¬×ÜÄÜ´¦ÀíÕâ¸ö´íÎó¡£¶ÔÓÚÒì³£ÇéÐΣ¬ÒѾÎÞ·¨¼ÌÐøÏÂÈ¥ÁË£¬ÒòΪÔÚµ±Ç°»·¾³ÏÂÎÞ·¨»ñµÃ±ØÒªµÄÐÅÏ¢À´½â¾öÎÊÌ⣬ÄãËùÄÜ×öµÄ¾ÍÊÇ´Óµ±Ç°»·¾³ÖÐÌø³ö£¬²¢°ÑÎÊÌâÌá½»¸øÉÏÒ»¼¶»·¾³£¬Õâ¾ÍÊÇÅ׳öÒ쳣ʱËù·¢ÉúµÄÊÂÇé¡£Å׳öÒì³£ºó£¬»áÓм¸¼þÊÂËæÖ®·¢Éú¡£Ê×ÏÈ£¬ÊÇÏñ´´½¨ÆÕͨµÄjava¶ÔÏóÒ»Ñù½«Ê¹ÓÃnewÔÚ¶ÑÉÏ´´½¨Ò»¸öÒì³£¶ÔÏó£»È»ºó£¬µ±Ç°µÄÖ´Ðз¾¶£¨ÒѾÎÞ·¨¼ÌÐøÏÂÈ¥ÁË£©±»ÖÕÖ¹£¬²¢ÇÒ´Óµ±Ç°»·¾³Öе¯³ö¶ÔÒì³£¶ÔÏóµÄÒýÓᣴËʱ£¬Òì³£´¦Àí»úÖÆ½Ó¹Ü³ÌÐò£¬²¢¿ªÊ¼Ñ°ÕÒÒ»¸öÇ¡µ±µÄµØ·½¼ÌÐøÖ´ÐгÌÐò£¬Õâ¸öÇ¡µ±µÄµØ·½¾ÍÊÇÒì³£´¦Àí³ÌÐò»òÕßÒì³£´¦ÀíÆ÷£¬ËüµÄÈÎÎñÊǽ«³ÌÐò´Ó´íÎó״̬Öлָ´£¬ÒÔʹ³ÌÐòҪô»»Ò»ÖÖ·½Ê½ÔËÐУ¬ÒªÃ´¼ÌÐøÔËÐÐÏÂÈ¥¡£
¾Ù¸ö¼òµ¥µÄÀý×Ó£¬¼ÙʹÎÒÃÇ´´½¨ÁËÒ»¸öѧÉú¶ÔÏóStudentµÄÒ»¸öÒýÓÃstu,ÔÚµ÷ÓõÄʱºò¿ÉÄÜ»¹Ã»Óгõʼ»¯¡£ËùÒÔÔÚʹÓÃÕâ¸ö¶ÔÏóÒýÓõ÷ÓÃÆäËû·½·¨Ö®Ç°£¬ÒªÏȶÔËü½øÐмì²é£¬¿ÉÒÔ´´½¨Ò»¸ö´ú±í´íÎóÐÅÏ¢µÄ¶ÔÏ󣬲¢ÇÒ½«Ëü´Óµ±Ç°»·¾³ÖÐÅ׳ö£¬ÕâÑù¾Í°Ñ´íÎóÐÅÏ¢´«²¥µ½¸ü´óµÄ»·¾³ÖС£
if(stu == null){
throw new NullPointerException();
} |
Õâ¾ÍÅ׳öÁËÒì³££¬Ëü½«ÔÚÆäËûµÄµØ·½µÃµ½Ö´ÐлòÕß´¦Àí£¬¾ßÌåÊÇÄĸöµØ·½ºóÃæ½«ºÜ¿ì½éÉÜ£¬´úÂëÖгöÏÖµÄ throw
ÊÇÒ»¸ö¹Ø¼ü×Ö£¬ÔÝʱÏȲ»×ö¹ý¶à½²½â£¬ºóÃæ»áÏêϸ½²½â¡£
²¶»ñÒì³££ºÔÚ·½·¨Å׳öÒì³£Ö®ºó£¬ÔËÐÐʱϵͳ½«×ªÎªÑ°ÕÒºÏÊʵÄÒì³£´¦ÀíÆ÷£¨exception handler£©¡£Ç±ÔÚµÄÒì³£´¦ÀíÆ÷ÊÇÒì³£·¢ÉúʱÒÀ´Î´æÁôÔÚµ÷ÓÃÕ»Öеķ½·¨µÄ¼¯ºÏ¡£µ±Òì³£´¦ÀíÆ÷ËùÄÜ´¦ÀíµÄÒì³£ÀàÐÍÓë·½·¨Å׳öµÄÒì³£ÀàÐÍÏà·ûʱ£¬¼´ÎªºÏÊʵÄÒì³£´¦ÀíÆ÷¡£ÔËÐÐʱϵͳ´Ó·¢ÉúÒì³£µÄ·½·¨¿ªÊ¼£¬ÒÀ´Î»Ø²éµ÷ÓÃÕ»Öеķ½·¨£¬Ö±ÖÁÕÒµ½º¬ÓкÏÊÊÒì³£´¦ÀíÆ÷µÄ·½·¨²¢Ö´ÐС£µ±ÔËÐÐʱϵͳ±éÀúµ÷ÓÃÕ»¶øÎ´ÕÒµ½ºÏÊʵÄÒì³£´¦ÀíÆ÷£¬ÔòÔËÐÐʱϵͳÖÕÖ¹¡£Í¬Ê±£¬Òâζ×ÅJava³ÌÐòµÄÖÕÖ¹¡£ Ìáʾ
¶ÔÓÚÔËÐÐʱÒì³£¡¢´íÎóºÍ¼ì²éÒì³££¬Java¼¼ÊõËùÒªÇóµÄÒì³£´¦Àí·½Ê½ÓÐËù²»Í¬¡£
ÓÉÓÚÔËÐÐʱÒì³£¼°Æä×ÓÀàµÄ²»¿É²éÐÔ£¬ÎªÁ˸üºÏÀí¡¢¸üÈÝÒ×µØÊµÏÖÓ¦ÓóÌÐò£¬Java¹æ¶¨£¬ÔËÐÐʱÒì³£½«ÓÉJavaÔËÐÐʱϵͳ×Ô¶¯Å׳ö£¬ÔÊÐíÓ¦ÓóÌÐòºöÂÔÔËÐÐʱÒì³£¡£
¶ÔÓÚ·½·¨ÔËÐÐÖпÉÄܳöÏÖµÄError£¬µ±ÔËÐз½·¨²»Óû²¶×½Ê±£¬JavaÔÊÐí¸Ã·½·¨²»×öÈκÎÅ׳öÉùÃ÷¡£ÒòΪ£¬´ó¶àÊýErrorÒì³£ÊôÓÚÓÀÔ¶²»Äܱ»ÔÊÐí·¢ÉúµÄ×´¿ö£¬Ò²ÊôÓÚºÏÀíµÄÓ¦ÓóÌÐò²»¸Ã²¶×½µÄÒì³£¡£
¶ÔÓÚËùÓеļì²éÒì³££¬Java¹æ¶¨£ºÒ»¸ö·½·¨±ØÐë²¶×½£¬»òÕßÉùÃ÷Å׳ö·½·¨Ö®Íâ¡£Ò²¾ÍÊÇ˵£¬µ±Ò»¸ö·½·¨Ñ¡Ôñ²»²¶×½¼ì²éÒ쳣ʱ£¬Ëü±ØÐëÉùÃ÷½«Å׳öÒì³£¡£
JavaÒì³£´¦ÀíÉæ¼°µ½Îå¸ö¹Ø¼ü×Ö£¬·Ö±ðÊÇ£ºtry¡¢catch¡¢finally¡¢throw¡¢throws¡£ÏÂÃæ½«ÖèÒ»½éÉÜ£¬Í¨¹ýÈÏʶÕâÎå¸ö¹Ø¼ü×Ö£¬ÕÆÎÕ»ù±¾Òì³£´¦Àí֪ʶ¡£
try -- ÓÃÓÚ¼àÌý¡£½«Òª±»¼àÌýµÄ´úÂë(¿ÉÄÜÅ׳öÒì³£µÄ´úÂë)·ÅÔÚtryÓï¾ä¿éÖ®ÄÚ£¬µ±tryÓï¾ä¿éÄÚ·¢ÉúÒ쳣ʱ£¬Òì³£¾Í±»Å׳ö¡£
catch -- ÓÃÓÚ²¶»ñÒì³£¡£catchÓÃÀ´²¶»ñtryÓï¾ä¿éÖз¢ÉúµÄÒì³£¡£
finally -- finallyÓï¾ä¿é×ÜÊǻᱻִÐС£ËüÖ÷ÒªÓÃÓÚ»ØÊÕÔÚtry¿éÀï´ò¿ªµÄÎïÁ¦×ÊÔ´(ÈçÊý¾Ý¿âÁ¬½Ó¡¢ÍøÂçÁ¬½ÓºÍ´ÅÅÌÎļþ)¡£Ö»ÓÐfinally¿é£¬Ö´ÐÐÍê³ÉÖ®ºó£¬²Å»á»ØÀ´Ö´ÐÐtry»òÕßcatch¿éÖеÄreturn»òÕßthrowÓï¾ä£¬Èç¹ûfinallyÖÐʹÓÃÁËreturn»òÕßthrowµÈÖÕÖ¹·½·¨µÄÓï¾ä£¬Ôò¾Í²»»áÌø»ØÖ´ÐУ¬Ö±½ÓÍ£Ö¹¡£
throw -- ÓÃÓÚÅ׳öÒì³£¡£
throws -- ÓÃÔÚ·½·¨Ç©ÃûÖУ¬ÓÃÓÚÉùÃ÷¸Ã·½·¨¿ÉÄÜÅ׳öµÄÒì³£¡£
Òì³£´¦ÀíµÄ»ù±¾Óï·¨
1. try-catch
try{
//code that might generate exceptions
}catch(Exception e){
//the code of handling exception1
}catch(Exception e){
//the code of handling exception2
} |
ÒªÃ÷°×Òì³£²¶»ñ£¬»¹ÒªÀí½â¼à¿ØÇøÓò£¨guarded region£©µÄ¸ÅÄî¡£ËüÊÇÒ»¶Î¿ÉÄܲúÉúÒì³£µÄ´úÂ룬²¢ÇÒºóÃæ¸ú×Å´¦ÀíÕâЩÒì³£µÄ´úÂë¡£
Òò¶ø¿ÉÖª£¬ÉÏÊötry-catchËùÃèÊöµÄ¼´ÊÇ¼à¿ØÇøÓò£¬¹Ø¼ü´ÊtryºóµÄÒ»¶Ô´óÀ¨ºÅ½«Ò»¿é¿ÉÄÜ·¢ÉúÒì³£µÄ´úÂë°üÆðÀ´£¬¼´Îª¼à¿ØÇøÓò¡£Java·½·¨ÔÚÔËÐйý³ÌÖз¢ÉúÁËÒì³££¬Ôò´´½¨Òì³£¶ÔÏó¡£½«Òì³£Å׳ö¼à¿ØÇøÓòÖ®Í⣬ÓÉJavaÔËÐÐʱϵͳ¸ºÔðѰÕÒÆ¥ÅäµÄcatch×Ó¾äÀ´²¶»ñÒì³£¡£ÈôÓÐÒ»¸öcatchÓï¾äÆ¥Åäµ½ÁË£¬ÔòÖ´ÐиÃcatch¿éÖеÄÒì³£´¦Àí´úÂ룬¾Í²»ÔÙ³¢ÊÔÆ¥Åä±ðµÄcatch¿éÁË¡£
Æ¥ÅäµÄÔÔòÊÇ£ºÈç¹ûÅ׳öµÄÒì³£¶ÔÏóÊôÓÚcatch×Ó¾äµÄÒì³£À࣬»òÕßÊôÓÚ¸ÃÒì³£ÀàµÄ×ÓÀ࣬ÔòÈÏΪÉú³ÉµÄÒì³£¶ÔÏóÓëcatch¿é²¶»ñµÄÒì³£ÀàÐÍÏàÆ¥Åä¡£
¾Ù¸öÀý×ÓËãÊõÒì³££º
public class
TestException {
public static void main(String[] args) {
int a = 1;
int b = 0;
try { // try¼à¿ØÇøÓò
if (b == 0) throw new ArithmeticException(); //
ͨ¹ýthrowÓï¾äÅ׳öÒì³£
System.out.println("a/bµÄÖµÊÇ£º" + a / b);
System.out.println("this will not be printed!");
}
catch (ArithmeticException e) { // catch²¶×½Òì³£
System.out.println("³ÌÐò³öÏÖÒì³££¬±äÁ¿b²»ÄÜΪ0£¡");
}
System.out.println("³ÌÐòÕý³£½áÊø¡£");
}
} |
ÔËÐнá¹û£º
D:\java>java
TestException
³ÌÐò³öÏÖÒì³££¬±äÁ¿b²»ÄÜΪ0£¡
³ÌÐòÕý³£½áÊø¡£ |
ÏÔʾһ¸öÒì³£µÄÃèÊö£¬ThrowableÖØÔØÁËtoString()·½·¨£¨ÓÉObject¶¨Ò壩£¬ËùÒÔËü½«·µ»ØÒ»¸ö°üº¬Òì³£ÃèÊöµÄ×Ö·û´®¡£ÀýÈ磬½«Ç°ÃæµÄcatch¿éÖØÐ´³É£º
catch (ArithmeticException
e) { // catch²¶×½Òì³£
System.out.println("³ÌÐò³öÏÖÒì³£"+e);
}
|
½á¹û£º
D:\java>java
TestException
³ÌÐò³öÏÖÒì³£java.lang.ArithmeticException
³ÌÐòÕý³£½áÊø¡£
|
¸ù¾ÝÇ°Ãæ½²ÊöµÄ£¬ËãÊõÒì³£ÊôÓÚÔËÐÐʱÒì³££¬Òò¶øÊµ¼ÊÉϸÃÒì³£²»ÐèÒª³ÌÐòÅ׳ö£¬ÔËÐÐʱϵͳ×Ô¶¯Å׳ö£¬½«Àý×Ó¸ÄΪÈçÏ£º
public class
TestException {
public static void main(String[] args) {
int a = 1;
int b = 0;
System.out.println("a/bµÄÖµÊÇ£º" + a / b);
System.out.println("this will not be printed!");
}
} |
½á¹û£º
D:\java>java
TestException
Exception in thread "main" java.lang.ArithmeticException:
/ by zero
at TestException.main(TestException.java:7) |
ʹÓöàÖØµÄcatchÓï¾ä£ººÜ¶àÇé¿öÏ£¬Óɵ¥¸öµÄ´úÂë¶Î¿ÉÄÜÒýÆð¶à¸öÒì³£¡£´¦ÀíÕâÖÖÇé¿ö£¬ÎÒÃÇÐèÒª¶¨ÒåÁ½¸ö»òÕ߸ü¶àµÄcatch×Ӿ䣬ÿ¸ö×Ӿ䲶»ñÒ»ÖÖÀàÐ͵ÄÒì³££¬µ±Òì³£±»Òý·¢Ê±£¬Ã¿¸öcatch×Ӿ䱻ÒÀ´Î¼ì²é£¬µÚÒ»¸öÆ¥ÅäÒì³£ÀàÐ͵Ä×Ó¾äÖ´ÐУ¬µ±Ò»¸öcatch×Ó¾äÖ´ÐÐÒÔºó£¬ÆäËûµÄ×Ӿ佫±»ÅÔ·¡£
±àд¶àÖØcatchÓï¾ä¿é×¢ÒâÊÂÏ
˳ÐòÎÊÌ⣺ÏÈСºó´ó£¬¼´ÏÈ×ÓÀàºó¸¸Àà
Javaͨ¹ýÒì³£ÀàÃèÊöÒì³£ÀàÐÍ¡£¶ÔÓÚÓжà¸öcatch×Ó¾äµÄÒì³£³ÌÐò¶øÑÔ£¬Ó¦¸Ã¾¡Á¿½«²¶»ñµ×²ãÒì³£ÀàµÄcatch×Ó¾ä·ÅÔÚÇ°Ãæ£¬Í¬Ê±¾¡Á¿½«²¶»ñÏà¶Ô¸ß²ãµÄÒì³£ÀàµÄcatch×Ó¾ä·ÅÔÚºóÃæ¡£·ñÔò£¬²¶»ñµ×²ãÒì³£ÀàµÄcatch×Ӿ佫¿ÉÄܻᱻÆÁ±Î¡£
RuntimeExceptionÒì³£Àà°üÀ¨ÔËÐÐʱ¸÷ÖÖ³£¼ûµÄÒì³££¬ArithmeticExceptionÀàºÍArrayIndexOutOfBoundsExceptionÀà¶¼ÊÇËüµÄ×ÓÀà¡£Òò´Ë£¬RuntimeExceptionÒì³£ÀàµÄcatch×Ó¾äÓ¦¸Ã·ÅÔÚ×îºóÃæ£¬·ñÔò¿ÉÄÜ»áÆÁ±ÎÆäºóµÄÌØ¶¨Òì³£´¦Àí»òÒýÆð±àÒë´íÎó¡£
ǶÌ×tryÓï¾ä£ºtryÓï¾ä¿ÉÒÔ±»Ç¶Ìס£Ò²¾ÍÊÇ˵£¬Ò»¸ötryÓï¾ä¿ÉÒÔÔÚÁíÒ»¸ötry¿éµÄÄÚ²¿¡£Ã¿´Î½øÈëtryÓï¾ä£¬Òì³£µÄǰºó¹ØÏµ¶¼»á±»ÍÆÈë¶ÑÕ»¡£Èç¹ûÒ»¸öÄÚ²¿µÄtryÓï¾ä²»º¬ÌØÊâÒì³£µÄcatch´¦Àí³ÌÐò£¬¶ÑÕ»½«µ¯³ö£¬ÏÂÒ»¸ötryÓï¾äµÄcatch´¦Àí³ÌÐò½«¼ì²éÊÇ·ñÓë֮ƥÅä¡£Õâ¸ö¹ý³Ì½«¼ÌÐøÖ±µ½Ò»¸öcatchÓï¾ä±»Æ¥Åä³É¹¦£¬»òÕßÊÇÖ±µ½ËùÓеÄǶÌ×tryÓï¾ä±»¼ì²éÍê±Ï¡£Èç¹ûûÓÐcatchÓï¾äÆ¥Å䣬JavaÔËÐÐʱϵͳ½«´¦ÀíÕâ¸öÒì³£¡£
ÀýÈ磺
class NestTry{
public static void main(String[] args){
try{
int a = args.length;
int b = 42 / a;
System.out.println("a = "+ a);
try{
if(a == 1){
a = a/(a-a);
}
if(a == 2){
int c[] = {1};
c[42] =99;
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds
:"+e);
}
}catch(ArithmeticException e){
System.out.println("Divide by 0"+ e);
}
}
} |
ÕýÈç³ÌÐòÖÐËùÏÔʾµÄ£¬¸Ã³ÌÐòÔÚÒ»¸ötry¿éÖÐǶÌ×ÁËÁíÒ»¸ötry¿é¡£³ÌÐò¹¤×÷ÈçÏ£ºµ±ÄãÔÚûÓÐÃüÁîÐвÎÊýµÄÇé¿öÏÂÖ´ÐиóÌÐò£¬ÍâÃæµÄtry¿é½«²úÉúÒ»¸ö±»0³ýµÄÒì³£¡£³ÌÐòÔÚÓÐÒ»¸öÃüÁîÐвÎÊýÌõ¼þÏÂÖ´ÐУ¬ÓÉǶÌ×µÄtry¿é²úÉúÒ»¸ö±»0³ýµÄÒì³££¬ÓÉÓÚÄÚ²¿µÄcatch¿é²»Æ¥ÅäÕâ¸öÒì³££¬Ëü½«°ÑÒì³£´«¸øÍⲿµÄtry¿é£¬ÔÚÍⲿÒì³£±»´¦Àí¡£Èç¹ûÄãÔÚ¾ßÓÐÁ½¸öÃüÁîÐвÎÊýµÄÌõ¼þÏÂÖ´ÐиóÌÐò£¬½«ÓÉÄÚ²¿try¿é²úÉúÒ»¸öÊý×é±ß½çÒì³£¡£
½á¹û£º
D:\java>javac
estTry.java
D:\java>>java NestTry
Divide by 0 java.lang.ArithmeticExceptio: /
by zero
D:\java>java NestTry one
a = 1
Divide by 0java.lang.ArithmeticException: /
by zero
D:\java>java NestTry one two
a = 2
ArrayIndexOutOfBounds :java.lang.ArrayIndexOutOfBoundsException:
42 |
×¢Ò⣺µ±Óз½·¨µ÷ÓÃʱ£¬tryÓï¾äµÄǶÌ׿ÉÒÔºÜÒþ±ÎµÄ·¢Éú¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½«¶Ô·½·¨µÄµ÷Ó÷ÅÔÚÒ»¸ötry¿éÖС£Ôڸ÷½·¨µÄÄÚ²¿£¬ÓÐÁíÒ»¸ötryÓï¾ä¡£ÔÚÕâÖÖÇé¿öÏ£¬·½·¨ÄÚ²¿µÄtryÈÔÈ»ÊÇǶÌ×ÔÚÍⲿµ÷Óø÷½·¨µÄtry¿éÖеġ£ÏÂÃæÎÒÃǽ«¶ÔÉÏÊöÀý×Ó½øÐÐÐ޸ģ¬Ç¶Ì×µÄtry¿éÒÆµ½·½·¨nesttry()µÄÄÚ²¿£º
class NestTry{
static void nesttry(int a){
try{
if(a == 1){
a = a/(a-a);
}
if(a == 2){
int c[] = {1};
c[42] =99;
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds
:"+e);
}
}
public static void main(String[] args){
try{
int a = args.length;
int b = 42 / a;
System.out.println("a = "+ a);
nesttry(a);
}catch(ArithmeticException e){
System.out.println("Divide by 0"+ e);
}
}
} |
½á¹ûÊä³öÓëÇ°ÃæÀý×ÓÒ»Ö£º
D:\java>javac
NestTry.java
D:\java>java NestTry
Divide by 0java.lang.ArithmeticException: /
by zero
D:\java>java NestTry one
a = 1
Divide by 0java.lang.ArithmeticException: /
by zero
D:\java>java NestTry one two
a = 2
ArrayIndexOutOfBounds :java.lang.ArrayIndexOutOfBoundsException:
42
} |
2. throw
µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃÇÖ»ÊÇ»ñÈ¡Á˱»JavaÔËÐÐʱϵͳÒý·¢µÄÒì³£¡£È»¶ø£¬ÎÒÃÇ»¹¿ÉÒÔÓÃthrowÓï¾äÅ׳öÃ÷È·µÄÒì³£¡£ThrowµÄÓï·¨ÐÎʽÈçÏ£º
ÕâÀïµÄThrowableInstanceÒ»¶¨ÊÇThrowableÀàÀàÐÍ»òÕßThrowable×ÓÀàÀàÐ͵ÄÒ»¸ö¶ÔÏó¡£¼òµ¥µÄÊý¾ÝÀàÐÍ£¬ÀýÈçint£¬char,ÒÔ¼°·ÇThrowableÀ࣬ÀýÈçString»òObject£¬²»ÄÜÓÃ×÷Òì³£¡£ÓÐÁ½ÖÖ·½·¨¿ÉÒÔ»ñÈ¡Throwable¶ÔÏó£ºÔÚcatch×Ó¾äÖÐʹÓòÎÊý»òÕßʹÓÃnew²Ù×÷·û´´½¨¡£
³ÌÐòÖ´ÐÐÍêthrowÓï¾äÖ®ºóÁ¢¼´Í£Ö¹£»throwºóÃæµÄÈκÎÓï¾ä²»±»Ö´ÐУ¬×îÁÚ½üµÄtry¿éÓÃÀ´¼ì²éËüÊÇ·ñº¬ÓÐÒ»¸öÓëÒì³£ÀàÐÍÆ¥ÅäµÄcatchÓï¾ä¡£Èç¹û·¢ÏÖÁËÆ¥ÅäµÄ¿é£¬¿ØÖÆ×ªÏò¸ÃÓï¾ä£»Èç¹ûûÓз¢ÏÖ£¬´Î°üΧµÄtry¿éÀ´¼ì²é£¬ÒÔ´ËÀàÍÆ¡£Èç¹ûûÓз¢ÏÖÆ¥ÅäµÄcatch¿é£¬Ä¬ÈÏÒì³£´¦Àí³ÌÐòÖжϳÌÐòµÄÖ´Ðв¢ÇÒ´òÓ¡¶ÑÕ»¹ì¼£¡£
ÀýÈ磺
class TestThrow{
static void proc(){
try{
throw new NullPointerException("demo");
}catch(NullPointerException e){
System.out.println("Caught inside proc");
throw e;
}
}
public static void main(String [] args){
try{
proc();
}catch(NullPointerException e){
System.out.println("Recaught: "+e);
}
}
} |
½á¹û£º
D:\java>java
TestThrow
Caught inside proc Recaught: java.lang.NullPointerException: demo |
¸Ã³ÌÐòÁ½´Î´¦ÀíÏàͬµÄ´íÎó£¬Ê×ÏÈ£¬main()·½·¨ÉèÁ¢ÁËÒ»¸öÒì³£¹ØÏµÈ»ºóµ÷ÓÃproc()¡£proc()·½·¨ÉèÁ¢ÁËÁíÒ»¸öÒì³£´¦Àí¹ØÏµ²¢ÇÒÁ¢¼´Å׳öÒ»¸öNullPointerExceptionʵÀý£¬NullPointerExceptionÔÚmain()Öб»Ôٴβ¶»ñ¡£
¸Ã³ÌÐò²ûÊöÁËÔõÑù´´½¨JavaµÄ±ê×¼Òì³£¶ÔÏó£¬Ìرð×¢ÒâÕâÒ»ÐУº
throw new NullPointerException("demo"); |
´Ë´¦newÓÃÀ´¹¹ÔìÒ»¸öNullPointerExceptionʵÀý£¬ËùÓеÄJavaÄÚÖõÄÔËÐÐʱÒì³£ÓÐÁ½¸ö¹¹Ôì·½·¨£ºÒ»¸öûÓвÎÊý£¬Ò»¸ö´øÓÐÒ»¸ö×Ö·û´®²ÎÊý¡£µ±ÓõڶþÖÖÐÎʽʱ£¬²ÎÊýÖ¸¶¨ÃèÊöÒì³£µÄ×Ö·û´®¡£Èç¹û¶ÔÏóÓÃ×÷print()»òÕßprintln()µÄ²ÎÊýʱ£¬¸Ã×Ö·û´®±»ÏÔʾ¡£ÕâͬÑù¿ÉÒÔͨ¹ýµ÷ÓÃgetMessage()À´ÊµÏÖ£¬getMessage()ÊÇÓÉThrowable¶¨ÒåµÄ¡£
3. throws Èç¹ûÒ»¸ö·½·¨¿ÉÒÔµ¼ÖÂÒ»¸öÒì³£µ«²»´¦ÀíËü£¬Ëü±ØÐëÖ¸¶¨ÕâÖÖÐÐΪÒÔʹ·½·¨µÄµ÷ÓÃÕß¿ÉÒÔ±£»¤ËüÃÇ×Ô¼º¶ø²»·¢ÉúÒì³£¡£Òª×öµ½Õâµã£¬ÎÒÃÇ¿ÉÒÔÔÚ·½·¨ÉùÃ÷Öаüº¬Ò»¸öthrows×Ӿ䡣һ¸öthrows×Ó¾äÁоÙÁËÒ»¸ö·½·¨¿ÉÄÜÒý·¢µÄËùÓÐÒì³£ÀàÐÍ¡£Õâ¶ÔÓÚ³ýÁËError»òRuntimeException¼°ËüÃÇ×ÓÀàÒÔÍâÀàÐ͵ÄËùÓÐÒì³£ÊDZØÒªµÄ¡£Ò»¸ö·½·¨¿ÉÒÔÒý·¢µÄËùÓÐÆäËûÀàÐ͵ÄÒì³£±ØÐëÔÚthrows×Ó¾äÖÐÉùÃ÷£¬·ñÔò»áµ¼Ö±àÒë´íÎó¡£
ÏÂÃæÊÇthrows×Ó¾äµÄ·½·¨ÉùÃ÷µÄͨÓÃÐÎʽ£º
public void
info() throws Exception
{
//body of method
} |
Exception ÊǸ÷½·¨¿ÉÄÜÒý·¢µÄËùÓеÄÒì³£,Ò²¿ÉÒÔÊÇÒì³£ÁÐ±í£¬ÖмäÒÔ¶ººÅ¸ô¿ª¡£
ÀýÈ磺
class TestThrows{
static void throw1(){
System.out.println("Inside throw1 . ");
throw new IllegalAccessException("demo");
}
public static void main(String[] args){
throw1();
}
} |
ÉÏÊöÀý×ÓÖÐÓÐÁ½¸öµØ·½´æÔÚ´íÎó£¬ÄãÄÜ¿´³öÀ´Âð£¿
¸ÃÀý×ÓÖдæÔÚÁ½¸ö´íÎó£¬Ê×ÏÈ£¬throw1()·½·¨²»Ïë´¦ÀíËùµ¼ÖµÄÒì³££¬Òò¶øËü±ØÐëÉùÃ÷throws×Ó¾äÀ´ÁоٿÉÄÜÒý·¢µÄÒì³£¼´IllegalAccessException£»Æä´Î£¬main()·½·¨±ØÐ붨Òåtry/catchÓï¾äÀ´²¶»ñ¸ÃÒì³£¡£
ÕýÈ·Àý×ÓÈçÏ£º
class TestThrows{
static void throw1() throws IllegalAccessException
{
System.out.println("Inside throw1 . ");
throw new IllegalAccessException("demo");
}
public static void main(String[] args){
try {
throw1();
}catch(IllegalAccessException e ){
System.out.println("Caught " + e);
}
}
} |
ThrowsÅ׳öÒì³£µÄ¹æÔò£º
Èç¹ûÊDz»Êܼì²éÒì³££¨unchecked exception£©£¬¼´Error¡¢RuntimeException»òËüÃǵÄ×ÓÀ࣬ÄÇô¿ÉÒÔ²»Ê¹ÓÃthrows¹Ø¼ü×ÖÀ´ÉùÃ÷ÒªÅ׳öµÄÒì³££¬±àÒëÈÔÄÜ˳Àûͨ¹ý£¬µ«ÔÚÔËÐÐʱ»á±»ÏµÍ³Å׳ö¡£ ±ØÐëÉùÃ÷·½·¨¿ÉÅ׳öµÄÈκμì²éÒì³££¨checked exception£©¡£¼´Èç¹ûÒ»¸ö·½·¨¿ÉÄܳöÏÖÊܿɲéÒì³££¬ÒªÃ´ÓÃtry-catchÓï¾ä²¶»ñ£¬ÒªÃ´ÓÃthrows×Ó¾äÉùÃ÷½«ËüÅ׳ö£¬·ñÔò»áµ¼Ö±àÒë´íÎó ½öµ±Å׳öÁËÒì³££¬¸Ã·½·¨µÄµ÷ÓÃÕ߲űØÐë´¦Àí»òÕßÖØÐÂÅ׳ö¸ÃÒì³£¡£µ±·½·¨µÄµ÷ÓÃÕßÎÞÁ¦´¦Àí¸ÃÒì³£µÄʱºò£¬Ó¦¸Ã¼ÌÐøÅ׳ö£¬¶ø²»ÊÇàñàðÍÌÔæ¡£ µ÷Ó÷½·¨±ØÐë×ñÑÈκοɲéÒì³£µÄ´¦ÀíºÍÉùÃ÷¹æÔò¡£Èô¸²¸ÇÒ»¸ö·½·¨£¬Ôò²»ÄÜÉùÃ÷Ó븲¸Ç·½·¨²»Í¬µÄÒì³£¡£ÉùÃ÷µÄÈκÎÒì³£±ØÐëÊDZ»¸²¸Ç·½·¨ËùÉùÃ÷Òì³£µÄͬÀà»ò×ÓÀà¡£ 4. finally µ±Òì³£·¢Éúʱ£¬Í¨³£·½·¨µÄÖ´Ðн«×öÒ»¸ö¶¸Ç͵ķÇÏßÐÔµÄתÏò£¬ËüÉõÖÁ»á¹ýÔçµÄµ¼Ö·½·¨·µ»Ø¡£ÀýÈ磬Èç¹ûÒ»¸ö·½·¨´ò¿ªÁËÒ»¸öÎļþ²¢¹Ø±Õ£¬È»ºóÍ˳ö£¬Ä㲻ϣÍû¹Ø±ÕÎļþµÄ´úÂë±»Òì³£´¦Àí»úÖÆÅÔ·¡£finally¹Ø¼ü×ÖΪ´¦ÀíÕâÖÖÒâÍâ¶øÉè¼Æ¡£
finally´´½¨µÄ´úÂë¿éÔÚtry/catch¿éÍê³ÉÖ®ºóÁíÒ»¸ötry/catch³öÏÖ֮ǰִÐС£finally¿éÎÞÂÛÓÐûÓÐÒì³£Å׳ö¶¼»áÖ´ÐС£Èç¹ûÅ׳öÒì³££¬¼´Ê¹Ã»ÓÐcatch×Ӿ䯥Å䣬finallyÒ²»áÖ´ÐС£Ò»¸ö·½·¨½«´ÓÒ»¸ötry/catch¿é·µ»Øµ½µ÷ÓóÌÐòµÄÈκÎʱºò£¬¾¹ýÒ»¸öδ²¶»ñµÄÒì³£»òÕßÊÇÒ»¸öÃ÷È·µÄ·µ»ØÓï¾ä£¬finally×Ó¾äÔÚ·½·¨·µ»ØÖ®Ç°ÈÔ½«Ö´ÐС£ÕâÔڹرÕÎļþ¾ä±úºÍÊÍ·ÅÈκÎÔÚ·½·¨¿ªÊ¼Ê±±»·ÖÅäµÄÆäËû×ÊÔ´ÊǺÜÓÐÓá£
finally×Ó¾äÊÇ¿ÉÑ¡Ï¿ÉÒÔÓÐÒ²¿ÉÒÔÎÞ£¬µ«ÊÇÿ¸ötryÓï¾äÖÁÉÙÐèÒªÒ»¸öcatch»òÕßfinally×Ӿ䡣
class TestFinally{
static void proc1(){
try{
System.out.println("inside proc1");
throw new RuntimeException("demo");
}finally{
System.out.println("proc1's finally");
}
}
static void proc2(){
try{
System.out.println("inside proc2");
return ;
} finally{
System.out.println("proc2's finally");
}
}
static void proc3(){
try{
System.out.println("inside proc3");
}finally{
System.out.println("proc3's finally");
}
}
public static void main(String [] args){
try{
proc1();
}catch(Exception e){
System.out.println("Exception caught");
}
proc2();
proc3();
}
} |
¸ÃÀý×ÓÖУ¬proc1()Å׳öÁËÒì³£ÖжÏÁËtry£¬ËüµÄfinally×Ó¾äÔÚÍ˳öʱִÐС£proc2µÄtryÓï¾äͨ¹ýreturnÓï¾ä·µ»Ø£¬µ«ÔÚ·µ»ØÖ®Ç°finallyÓï¾äÖ´ÐС£ÔÚproc3()ÖÐtryÓï¾äÕý³£Ö´ÐУ¬Ã»ÓдíÎó£¬finallyÓï¾äÒ²±»Ö´ÐС£
Êä³ö½á¹û£º
D:\java>java
TestFinally
inside proc1
proc1's finally
Exception caught
inside proc2
proc2's finally
inside proc3
proc3's finally |
×¢£ºÈç¹ûfinally¿éÓëÒ»¸ötryÁªºÏʹÓã¬finally¿é½«ÔÚtry½áÊøÖ®Ç°Ö´ÐС£
ÎÊÌâÀ©Õ¹£¨ÃæÊÔÌ⣩£º 1¡¢try{} ÀïÓÐÒ»¸ö return Óï¾ä£¬ÄÇô½ô¸úÔÚÕâ¸ö try ºóµÄ finally{} ÀïµÄ
code »á²»»á±»Ö´ÐУ¬Ê²Ã´Ê±ºò±»Ö´ÐУ¬ÔÚ return ǰ»¹ÊǺó?
´ð°¸£º»áÖ´ÐУ¬ÔÚ·½·¨·µ»Øµ÷ÓÃÕßǰִÐС£
×¢Ò⣺ÔÚfinallyÖиı䷵»ØÖµµÄ×ö·¨ÊDz»ºÃµÄ£¬ÒòΪÈç¹û´æÔÚfinally´úÂë¿é£¬tryÖеÄreturnÓï¾ä²»»áÁ¢Âí·µ»Øµ÷ÓÃÕߣ¬¶øÊǼǼϷµ»ØÖµ´ýfinally´úÂë¿éÖ´ÐÐÍê±ÏÖ®ºóÔÙÏòµ÷ÓÃÕß·µ»ØÆäÖµ£¬È»ºóÈç¹ûÔÚfinallyÖÐÐÞ¸ÄÁË·µ»ØÖµ£¬¾Í»á·µ»ØÐ޸ĺóµÄÖµ¡£ÏÔÈ»£¬ÔÚfinallyÖзµ»Ø»òÕßÐ޸ķµ»ØÖµ»á¶Ô³ÌÐòÔì³ÉºÜ´óµÄÀ§ÈÅ£¬C#ÖÐÖ±½ÓÓñàÒë´íÎóµÄ·½Ê½À´×èÖ¹³ÌÐòÔ±¸ÉÕâÖÖö»öºµÄÊÂÇ飬JavaÖÐÒ²¿ÉÒÔͨ¹ýÌáÉý±àÒëÆ÷µÄÓï·¨¼ì²é¼¶±ðÀ´²úÉú¾¯¸æ»ò´íÎó£¬EclipseÖпÉÒÔÔÚÈçͼËùʾµÄµØ·½½øÐÐÉèÖã¬Ç¿ÁÒ½¨Ò齫´ËÏîÉèÖÃΪ±àÒë´íÎó¡£

2¡¢JavaÓïÑÔÈçºÎ½øÐÐÒì³£´¦Àí£¬¹Ø¼ü×Ö£ºthrows¡¢throw¡¢try¡¢catch¡¢finally·Ö±ðÈçºÎʹÓã¿
´ð£ºJavaͨ¹ýÃæÏò¶ÔÏóµÄ·½·¨½øÐÐÒì³£´¦Àí£¬°Ñ¸÷ÖÖ²»Í¬µÄÒì³£½øÐзÖÀ࣬²¢ÌṩÁËÁ¼ºÃµÄ½Ó¿Ú¡£ÔÚJavaÖУ¬Ã¿¸öÒì³£¶¼ÊÇÒ»¸ö¶ÔÏó£¬ËüÊÇThrowableÀà»òÆä×ÓÀàµÄʵÀý¡£µ±Ò»¸ö·½·¨³öÏÖÒì³£ºó±ãÅ׳öÒ»¸öÒì³£¶ÔÏ󣬸öÔÏóÖаüº¬ÓÐÒì³£ÐÅÏ¢£¬µ÷ÓÃÕâ¸ö¶ÔÏóµÄ·½·¨¿ÉÒÔ²¶»ñµ½Õâ¸öÒì³£²¢¿ÉÒÔ¶ÔÆä½øÐд¦Àí¡£JavaµÄÒì³£´¦ÀíÊÇͨ¹ý5¸ö¹Ø¼ü´ÊÀ´ÊµÏֵģºtry¡¢catch¡¢throw¡¢throwsºÍfinally¡£Ò»°ãÇé¿öÏÂÊÇÓÃtryÀ´Ö´ÐÐÒ»¶Î³ÌÐò£¬Èç¹ûϵͳ»áÅ׳ö£¨throw£©Ò»¸öÒì³£¶ÔÏ󣬿ÉÒÔͨ¹ýËüµÄÀàÐÍÀ´²¶»ñ£¨catch£©Ëü£¬»òͨ¹ý×ÜÊÇÖ´ÐдúÂë¿é£¨finally£©À´´¦Àí£»tryÓÃÀ´Ö¸¶¨Ò»¿éÔ¤·ÀËùÓÐÒì³£µÄ³ÌÐò£»catch×Ó¾ä½ô¸úÔÚtry¿éºóÃæ£¬ÓÃÀ´Ö¸¶¨ÄãÏëÒª²¶»ñµÄÒì³£µÄÀàÐÍ£»throwÓï¾äÓÃÀ´Ã÷È·µØÅ׳öÒ»¸öÒì³££»throwsÓÃÀ´ÉùÃ÷Ò»¸ö·½·¨¿ÉÄÜÅ׳öµÄ¸÷ÖÖÒì³££¨µ±È»ÉùÃ÷Ò쳣ʱÔÊÐíÎÞ²¡ÉëÒ÷£©£»finallyΪȷ±£Ò»¶Î´úÂë²»¹Ü·¢ÉúʲôÒì³£×´¿ö¶¼Òª±»Ö´ÐУ»tryÓï¾ä¿ÉÒÔǶÌ×£¬Ã¿µ±Óöµ½Ò»¸ötryÓï¾ä£¬Òì³£µÄ½á¹¹¾Í»á±»·ÅÈëÒì³£Õ»ÖУ¬Ö±µ½ËùÓеÄtryÓï¾ä¶¼Íê³É¡£Èç¹ûÏÂÒ»¼¶µÄtryÓï¾äûÓжÔijÖÖÒì³£½øÐд¦Àí£¬Òì³£Õ»¾Í»áÖ´ÐгöÕ»²Ù×÷£¬Ö±µ½Óöµ½Óд¦ÀíÕâÖÖÒì³£µÄtryÓï¾ä»òÕß×îÖÕ½«Òì³£Å׸øJVM¡£
3¡¢ÔËÐÐʱÒì³£ÓëÊܼìÒì³£ÓкÎÒìͬ£¿
´ð£ºÒì³£±íʾ³ÌÐòÔËÐйý³ÌÖпÉÄܳöÏֵķÇÕý³£×´Ì¬£¬ÔËÐÐʱÒì³£±íʾÐéÄâ»úµÄͨ³£²Ù×÷ÖпÉÄÜÓöµ½µÄÒì³££¬ÊÇÒ»ÖÖ³£¼ûÔËÐдíÎó£¬Ö»Òª³ÌÐòÉè¼ÆµÃûÓÐÎÊÌâͨ³£¾Í²»»á·¢Éú¡£ÊܼìÒì³£¸ú³ÌÐòÔËÐеÄÉÏÏÂÎÄ»·¾³Óйأ¬¼´Ê¹³ÌÐòÉè¼ÆÎÞÎó£¬ÈÔÈ»¿ÉÄÜÒòʹÓõÄÎÊÌâ¶øÒý·¢¡£Java±àÒëÆ÷ÒªÇó·½·¨±ØÐëÉùÃ÷Å׳ö¿ÉÄÜ·¢ÉúµÄÊܼìÒì³££¬µ«ÊDz¢²»ÒªÇó±ØÐëÉùÃ÷Å׳öδ±»²¶»ñµÄÔËÐÐʱÒì³£¡£Òì³£ºÍ¼Ì³ÐÒ»Ñù£¬ÊÇÃæÏò¶ÔÏó³ÌÐòÉè¼ÆÖо³£±»ÀÄÓõĶ«Î÷£¬ÔÚEffective
JavaÖжÔÒì³£µÄʹÓøø³öÁËÒÔÏÂÖ¸µ¼ÔÔò£º - ²»Òª½«Òì³£´¦ÀíÓÃÓÚÕý³£µÄ¿ØÖÆÁ÷£¨Éè¼ÆÁ¼ºÃµÄAPI²»Ó¦¸ÃÇ¿ÆÈËüµÄµ÷ÓÃÕßΪÁËÕý³£µÄ¿ØÖÆÁ÷¶øÊ¹ÓÃÒì³££©
- ¶Ô¿ÉÒÔ»Ö¸´µÄÇé¿öʹÓÃÊܼìÒì³££¬¶Ô±à³Ì´íÎóʹÓÃÔËÐÐʱÒì³£ - ±ÜÃâ²»±ØÒªµÄʹÓÃÊܼìÒì³££¨¿ÉÒÔͨ¹ýһЩ״̬¼ì²âÊÖ¶ÎÀ´±ÜÃâÒì³£µÄ·¢Éú£© - ÓÅÏÈʹÓñê×¼µÄÒì³£ - ÿ¸ö·½·¨Å׳öµÄÒì³£¶¼ÒªÓÐÎĵµ - ±£³ÖÒì³£µÄÔ×ÓÐÔ - ²»ÒªÔÚcatchÖкöÂÔµô²¶»ñµ½µÄÒì³£
4¡¢ÁгöһЩÄã³£¼ûµÄÔËÐÐʱÒì³££¿
´ð£º - ArithmeticException£¨ËãÊõÒì³££© - ClassCastException £¨Ààת»»Òì³££© - IllegalArgumentException £¨·Ç·¨²ÎÊýÒì³££© - IndexOutOfBoundsException £¨Ï±êÔ½½çÒì³££© - NullPointerException £¨¿ÕÖ¸ÕëÒì³££© - SecurityException £¨°²È«Òì³££©

Òì³£Á´
Òì³£Á´¹ËÃû˼Òå¾ÍÊǽ«Òì³£·¢ÉúµÄÔÒòÒ»¸ö´«Ò»¸ö´®ÆðÀ´£¬¼´°Ñµ×²ãµÄÒì³£ÐÅÏ¢´«¸øÉϲ㣬ÕâÑùÖð²ãÅ׳ö¡£
Java APIÎĵµÖиø³öÁËÒ»¸ö¼òµ¥µÄÄ£ÐÍ£º
try {
lowLevelOp();
} catch (LowLevelException le) {
throw (HighLevelException) new HighLevelException().initCause(le);
} |
µ±³ÌÐò²¶»ñµ½ÁËÒ»¸öµ×²ãÒì³££¬ÔÚ´¦Àí²¿·ÖÑ¡ÔñÁ˼ÌÐøÅ׳öÒ»¸ö¸ü¸ß¼¶±ðµÄÐÂÒì³£¸ø´Ë·½·¨µÄµ÷ÓÃÕß¡£ ÕâÑùÒì³£µÄÔÒò¾Í»áÖð²ã´«µÝ¡£ÕâÑù£¬Î»Óڸ߲ãµÄÒì³£µÝ¹éµ÷ÓÃgetCause()·½·¨£¬¾Í¿ÉÒÔ±éÀú¸÷²ãµÄÒì³£ÔÒò¡£
Õâ¾ÍÊÇJavaÒì³£Á´µÄÔÀí¡£Òì³£Á´µÄʵ¼ÊÓ¦ÓúÜÉÙ£¬·¢ÉúÒ쳣ʱºòÖð²ãÉÏÅײ»ÊǸöºÃ×¢Ò⣬ ÉϲãÄõ½ÕâЩÒì³£ÓÖÄÜÄÎÖ®ºÎ£¿¶øÇÒÒì³£Öð²ãÉÏÅ×»áÏûºÄ´óÁ¿×ÊÔ´£¬
ÒòΪҪ±£´æÒ»¸öÍêÕûµÄÒì³£Á´ÐÅÏ¢.
×Ô¶¨ÒåÒì³£ ʹÓÃJavaÄÚÖõÄÒì³£Àà¿ÉÒÔÃèÊöÔÚ±à³Ìʱ³öÏֵĴ󲿷ÖÒì³£Çé¿ö¡£³ý´ËÖ®Í⣬Óû§»¹¿ÉÒÔ×Ô¶¨ÒåÒì³£¡£Óû§×Ô¶¨ÒåÒì³£Àֻ࣬Ðè¼Ì³ÐExceptionÀ༴¿É¡£
ÔÚ³ÌÐòÖÐʹÓÃ×Ô¶¨ÒåÒì³£À࣬´óÌå¿É·ÖΪÒÔϼ¸¸ö²½Öè:
´´½¨×Ô¶¨ÒåÒì³£Àà¡£ ÔÚ·½·¨ÖÐͨ¹ýthrow¹Ø¼ü×ÖÅ׳öÒì³£¶ÔÏó¡£ Èç¹ûÔÚµ±Ç°Å׳öÒì³£µÄ·½·¨Öд¦ÀíÒì³££¬¿ÉÒÔʹÓÃtry-catchÓï¾ä²¶»ñ²¢´¦Àí£»·ñÔòÔÚ·½·¨µÄÉùÃ÷´¦Í¨¹ýthrows¹Ø¼ü×ÖÖ¸Ã÷ÒªÅ׳ö¸ø·½·¨µ÷ÓÃÕßµÄÒì³££¬¼ÌÐø½øÐÐÏÂÒ»²½²Ù×÷¡£ ÔÚ³öÏÖÒì³£·½·¨µÄµ÷ÓÃÕßÖв¶»ñ²¢´¦ÀíÒì³£¡£
¾ÙÀý×Ô¶¨ÒåÒì³££º
class MyException
extends Exception {
private int detail;
MyException(int a){
detail = a;
}
public String toString(){
return "MyException ["+ detail + "]";
}
}
public class TestMyException{
static void compute(int a) throws MyException{
System.out.println("Called compute("
+ a + ")");
if(a > 10){
throw new MyException(a);
}
System.out.println("Normal exit!");
}
public static void main(String [] args){
try{
compute(1);
compute(20);
}catch(MyException me){
System.out.println("Caught " + me);
}
}
} |
¸ÃÀý×ÓÍêÈ«°´ÕÕÉÏÊö²½Öè¡£
ÔËÐнá¹ûÈçÏ£º
D:\java>java TestMyException
Called compute(1)
Normal exit!
Called compute(20)
Caught MyException [20] |
×ܽá


|