缁忔祹瑙傚療
杩愯惀绠$悊

java 寮傚父绠$悊

java 寮傚父绠$悊

java涓紓甯哥殑澶勭悊鏂规硶鏈夊摢涓ょ

鍙互閫夋嫨涓夌鏂规硶鏉ヨ繘琛屽紓甯稿鐞?瀵逛唬鐮佸潡鐢╰ry..catch杩涜寮傚父鎹曡幏澶勭悊锛?鍦?璇ヤ唬鐮佺殑鏂规硶浣撳鐢╰hrows杩涜鎶涘嚭澹版槑锛屽憡鐭ユ鏂规硶鐨勮皟鐢ㄨ€呰繖娈典唬鐮佸彲鑳戒細鍑虹幇杩欎簺寮傚父锛屼綘闇€瑕佽皑鎱庡鐞嗐€?/p>

姝ゆ椂鏈変袱绉嶆儏鍐碉細濡傛灉澹版槑鎶涘嚭鐨勫紓甯告槸闈炶繍琛屾椂寮傚父锛屾鏂规硶鐨勮皟鐢ㄨ€呭繀椤绘樉绀哄湴鐢╰ry..catch鍧楄繘琛屾崟鑾锋垨鑰呯户缁悜涓婂眰鎶涘嚭寮傚父銆?/p>

濡傛灉澹版槑鎶涘嚭鐨勫紓甯告槸杩愯鏃跺紓甯革紝姝ゆ柟娉曠殑璋冪敤鑰呭彲浠ラ€夋嫨鍦拌繘琛屽紓甯告崟鑾峰鐞嗐€?/p>

3鍦ㄤ唬鐮佸潡鐢╰hrow鎵嬪姩鎶涘嚭涓€涓紓甯稿璞★紝姝ゆ椂涔熸湁涓ょ鎯呭喌锛岃窡2锛変腑鐨勭被浼硷細濡傛灉鎶涘嚭鐨勫紓甯稿璞℃槸闈炶繍琛屾椂寮傚父锛屾鏂规硶鐨勮皟鐢ㄨ€呭繀椤绘樉绀哄湴鐢╰ry..catch鍧楄繘琛屾崟鑾锋垨鑰呯户缁悜涓婂眰鎶涘嚭寮傚父銆?/p>

濡傛灉鎶涘嚭鐨勫紓甯稿璞℃槸杩愯鏃跺紓甯革紝姝ゆ柟娉曠殑璋冪敤鑰呭彲浠ラ€夋嫨鍦拌繘琛屽紓甯告崟鑾峰鐞嗐€?/p>

锛堝鏋滄渶缁堝皢寮傚父鎶涚粰main鏂规硶锛屽垯鐩稿綋浜庝氦缁檍vm鑷姩澶勭悊锛屾鏃秊vm浼氱畝鍗曞湴鎵撳嵃寮傚父淇℃伅锛夊父澶勭悊鍜岃璁$殑寤鸿1. 鍙湪蹇呰浣跨敤寮傚父鐨勫湴鏂规墠浣跨敤寮傚父锛屼笉瑕佺敤寮傚父鍘绘帶鍒剁▼搴忕殑娴佺▼2. 鍒囧繉浣跨敤绌篶atch鍧?. 妫€鏌ュ紓甯稿拰闈炴鏌ュ紓甯哥殑閫夋嫨4. 娉ㄦ剰catch鍧楃殑椤哄簭5. 涓嶈灏嗘彁渚涚粰鐢ㄦ埛鐪嬬殑淇℃伅鏀惧湪寮傚父淇℃伅閲?. 閬垮厤澶氭鍦ㄦ棩蹇椾俊鎭腑璁板綍鍚屼竴涓紓甯?. 寮傚父澶勭悊灏介噺鏀惧湪楂樺眰杩涜8. 鍦╢inally涓噴鏀捐祫婧?/p>

璋佺煡閬揓AVA寮傚父澶勭悊鏈哄埗鐨勪娇鐢ㄦ妧宸э紒

Java寮傚父瀛︿範蹇冨緱 鏈枃閲嶅湪Java涓紓甯告満鍒剁殑涓€浜涙蹇点€?/p>

鍐欐湰鏂囩殑鐩殑鍦ㄤ簬鏂逛究鎴戝緢闀挎椂闂村悗鑻ユ槸蹇樹簡杩欎簺涓滆タ鍙互閫氳繃杩欑墖鏂囩珷杩呴€熷洖蹇嗚捣鏉ャ€?/p>

1. 寮傚父鏈哄埗 1.1 寮傚父鏈哄埗鏄寚褰撶▼搴忓嚭鐜伴敊璇悗锛岀▼搴忓浣曞鐞嗐€?/p>

鍏蜂綋鏉ヨ锛屽紓甯告満鍒舵彁渚涗簡绋嬪簭閫€鍑虹殑瀹夊叏閫氶亾銆?/p>

褰撳嚭鐜伴敊璇悗锛岀▼搴忔墽琛岀殑娴佺▼鍙戠敓鏀瑰彉锛岀▼搴忕殑鎺у埗鏉冭浆绉诲埌寮傚父澶勭悊鍣ㄣ€?/p>

1.2 浼犵粺鐨勫鐞嗗紓甯哥殑鍔炴硶鏄紝鍑芥暟杩斿洖涓€涓壒娈婄殑缁撴灉鏉ヨ〃绀哄嚭鐜板紓甯革紙閫氬父杩欎釜鐗规畩缁撴灉鏄ぇ瀹剁害瀹氫織绉扮殑锛夛紝璋冪敤璇ュ嚱鏁扮殑绋嬪簭璐熻矗妫€鏌ュ苟鍒嗘瀽鍑芥暟杩斿洖鐨勭粨鏋溿€?/p>

杩欐牱鍋氭湁濡備笅鐨勫紛绔細渚嬪鍑芥暟杩斿洖-1浠h〃鍑虹幇寮傚父锛屼絾鏄鏋滃嚱鏁扮‘瀹炶杩斿洖-1杩欎釜姝g‘鐨勫€兼椂灏变細鍑虹幇娣锋穯锛涘彲璇绘€ч檷浣庯紝灏嗙▼搴忎唬鐮佷笌澶勭悊寮傚父鐨勪唬鐮佹贩鐖瑰湪涓€璧凤紱鐢辫皟鐢ㄥ嚱鏁扮殑绋嬪簭鏉ュ垎鏋愰敊璇紝杩欏氨瑕佹眰瀹㈡埛绋嬪簭鍛樺搴撳嚱鏁版湁寰堟繁鐨勪簡瑙c€?/p>

1.3 寮傚父澶勭悊鐨勬祦绋?1.3.1 閬囧埌閿欒锛屾柟娉曠珛鍗崇粨鏉燂紝骞朵笉杩斿洖涓€涓€硷紱鍚屾椂锛屾姏鍑轰竴涓紓甯稿璞?1.3.2 璋冪敤璇ユ柟娉曠殑绋嬪簭涔熶笉浼氱户缁墽琛屼笅鍘伙紝鑰屾槸鎼滅储涓€涓彲浠ュ鐞嗚寮傚父鐨勫紓甯稿鐞嗗櫒锛屽苟鎵ц鍏朵腑鐨勪唬鐮?2 寮傚父鐨勫垎绫?2.1 寮傚父鐨勫垎绫?2.1.1 寮傚父鐨勭户鎵跨粨鏋勶細鍩虹被涓篢hrowable,Error鍜孍xception缁ф壙Throwable,RuntimeException鍜孖OException绛夌户鎵縀xception锛屽叿浣撶殑RuntimeException缁ф壙RuntimeException銆?/p>

2.1.2 Error鍜孯untimeException鍙婂叾瀛愮被鎴愪负鏈鏌ュ紓甯革紙unchecked锛夛紝鍏跺畠寮傚父鎴愪负宸叉鏌ュ紓甯革紙checked锛夈€?/p>

2.2 姣忎釜绫诲瀷鐨勫紓甯哥殑鐗圭偣 2.2.1 Error浣撶郴 Error绫讳綋绯绘弿杩颁簡Java杩愯绯荤粺涓殑鍐呴儴閿欒浠ュ強璧勬簮鑰楀敖鐨勬儏褰€?/p>

搴旂敤绋嬪簭涓嶅簲璇ユ姏鍑鸿繖绉嶇被鍨嬬殑瀵硅薄锛堜竴鑸槸鐢辫櫄鎷熸満鎶涘嚭锛夈€?/p>

濡傛灉鍑虹幇杩欑閿欒锛岄櫎浜嗗敖鍔涗娇绋嬪簭瀹夊叏閫€鍑哄锛屽湪鍏朵粬鏂归潰鏄棤鑳戒负鍔涚殑銆?/p>

鎵€浠ワ紝鍦ㄨ繘琛岀▼搴忚璁℃椂锛屽簲璇ユ洿鍏虫敞Exception浣撶郴銆?/p>

2.2.2 Exception浣撶郴 Exception浣撶郴鍖呮嫭RuntimeException浣撶郴鍜屽叾浠栭潪RuntimeException鐨勪綋绯?2.2.2.1 RuntimeException RuntimeException浣撶郴鍖呮嫭閿欒鐨勭被鍨嬭浆鎹€佹暟缁勮秺鐣岃闂拰璇曞浘璁块棶绌烘寚閽堢瓑绛夈€?/p>

澶勭悊RuntimeException鐨勫師鍒欐槸锛氬鏋滃嚭鐜癛untimeException锛岄偅涔堜竴瀹氭槸绋嬪簭鍛樼殑閿欒銆?/p>

渚嬪锛屽彲浠ラ€氳繃妫€鏌ユ暟缁勪笅鏍囧拰鏁扮粍杈圭晫鏉ラ伩鍏嶆暟缁勮秺鐣岃闂紓甯搞€?/p>

2.2.2.2 鍏朵粬锛圛OException绛夌瓑锛?杩欑被寮傚父涓€鑸槸澶栭儴閿欒锛屼緥濡傝瘯鍥句粠鏂囦欢灏惧悗璇诲彇鏁版嵁绛夛紝杩欏苟涓嶆槸绋嬪簭鏈韩鐨勯敊璇紝鑰屾槸鍦ㄥ簲鐢ㄧ幆澧冧腑鍑虹幇鐨勫閮ㄩ敊璇€?/p>

2.3 涓嶤++寮傚父鍒嗙被鐨勪笉鍚?2.3.1 鍏跺疄锛孞ava涓璕untimeException杩欎釜绫诲悕璧风殑骞朵笉鎭板綋锛屽洜涓轰换浣曞紓甯搁兘鏄繍琛屾椂鍑虹幇鐨勩€?/p>

锛堝湪缂栬瘧鏃跺嚭鐜扮殑閿欒骞朵笉鏄紓甯革紝鎹㈠彞璇濊锛屽紓甯稿氨鏄负浜嗚В鍐崇▼搴忚繍琛屾椂鍑虹幇鐨勭殑閿欒锛夈€?/p>

2.3.2 C++涓璴ogic_error涓嶫ava涓殑RuntimeException鏄瓑浠风殑锛岃€宺untime_error涓嶫ava涓潪RuntimeException绫诲瀷鐨勫紓甯告槸绛変环鐨勩€?/p>

3 寮傚父鐨勪娇鐢ㄦ柟娉?3.1 澹版槑鏂规硶鎶涘嚭寮傚父 3.1.1 璇硶锛歵hrows锛堢暐锛?3.1.2 涓轰粈涔堣澹版槑鏂规硶鎶涘嚭寮傚父锛?鏂规硶鏄惁鎶涘嚭寮傚父涓庢柟娉曡繑鍥炲€肩殑绫诲瀷涓€鏍烽噸瑕併€?/p>

鍋囪鏂规硶鎶涘嚭寮傚父纭病鏈夊0鏄庤鏂规硶灏嗘姏鍑哄紓甯革紝閭d箞瀹㈡埛绋嬪簭鍛樺彲浠ヨ皟鐢ㄨ繖涓柟娉曡€屼笖涓嶇敤缂栧啓澶勭悊寮傚父鐨勪唬鐮併€?/p>

閭d箞锛屼竴鏃﹀嚭鐜板紓甯革紝閭d箞杩欎釜寮傚父灏辨病鏈夊悎閫傜殑寮傚父鎺у埗鍣ㄦ潵瑙e喅銆?/p>

3.1.3 涓轰粈涔堟姏鍑虹殑寮傚父涓€瀹氭槸宸叉鏌ュ紓甯革紵 RuntimeException涓嶦rror鍙互鍦ㄤ换浣曚唬鐮佷腑浜х敓锛屽畠浠笉闇€瑕佺敱绋嬪簭鍛樻樉绀虹殑鎶涘嚭锛屼竴鏃﹀嚭鐜伴敊璇紝閭d箞鐩稿簲鐨勫紓甯镐細琚嚜鍔ㄦ姏鍑恒€?/p>

鑰屽凡妫€鏌ュ紓甯告槸鐢辩▼搴忓憳鎶涘嚭鐨勶紝杩欏垎涓轰袱绉嶆儏鍐碉細瀹㈡埛绋嬪簭鍛樿皟鐢ㄤ細鎶涘嚭寮傚父鐨勫簱鍑芥暟锛堝簱鍑芥暟鐨勫紓甯哥敱搴撶▼搴忓憳鎶涘嚭锛夛紱瀹㈡埛绋嬪簭鍛樿嚜宸变娇鐢╰hrow璇彞鎶涘嚭寮傚父銆?/p>

閬囧埌Error锛岀▼搴忓憳涓€鑸槸鏃犺兘涓哄姏鐨勶紱閬囧埌RuntimeException锛岄偅涔堜竴瀹氭槸绋嬪簭瀛樺湪閫昏緫閿欒锛岃瀵圭▼搴忚繘琛屼慨鏀癸紙鐩稿綋浜庤皟璇曠殑涓€绉嶆柟娉曪級锛涘彧鏈夊凡妫€鏌ュ紓甯告墠鏄▼搴忓憳鎵€鍏冲績鐨勶紝绋嬪簭搴旇涓斾粎搴旇鎶涘嚭鎴栧鐞嗗凡妫€鏌ュ紓甯搞€?/p>

3.1.4 娉ㄦ剰锛氳鐩栫埗绫绘煇鏂规硶鐨勫瓙绫绘柟娉曚笉鑳芥姏鍑烘瘮鐖剁被鏂规硶鏇村鐨勫紓甯革紝鎵€浠ワ紝鏈夋椂璁捐鐖剁被鐨勬柟娉曟椂浼氬0鏄庢姏鍑哄紓甯革紝浣嗗疄闄呯殑瀹炵幇鏂规硶鐨勪唬鐮佸嵈骞朵笉鎶涘嚭寮傚父锛岃繖鏍峰仛鐨勭洰鐨勫氨鏄负浜嗘柟渚垮瓙绫绘柟娉曡鐩栫埗绫绘柟娉曟椂鍙互鎶涘嚭寮傚父銆?/p>

3.2 濡備綍鎶涘嚭寮傚父 3.2.1 璇硶锛歵hrow锛堢暐锛?3.2.2 鎶涘嚭浠€涔堝紓甯革紵 瀵逛簬涓€涓紓甯稿璞★紝鐪熸鏈夌敤鐨勪俊鎭椂寮傚父鐨勫璞$被鍨嬶紝鑰屽紓甯稿璞℃湰韬鏃犳剰涔夈€?/p>

姣斿涓€涓紓甯稿璞$殑绫诲瀷鏄疌lassCastException锛岄偅涔堣繖涓被鍚嶅氨鏄敮涓€鏈夌敤鐨勪俊鎭€?/p>

鎵€浠ワ紝鍦ㄩ€夋嫨鎶涘嚭浠€涔堝紓甯告椂锛屾渶鍏抽敭鐨勫氨鏄€夋嫨寮傚父鐨勭被鍚嶈兘澶熸槑纭鏄庡紓甯告儏鍐电殑绫汇€?/p>

3.2.3 寮傚父瀵硅薄閫氬父鏈変袱绉嶆瀯閫犲嚱鏁帮細涓€绉嶆槸鏃犲弬鏁扮殑鏋勯€犲嚱鏁帮紱鍙︿竴绉嶆槸甯︿竴涓瓧绗︿覆鐨勬瀯閫犲嚱鏁帮紝杩欎釜瀛楃涓插皢浣滀负杩欎釜寮傚父瀵硅薄闄や簡绫诲瀷鍚嶄互澶栫殑棰濆璇存槑銆?/p>

3.2.4 鍒涘缓鑷繁鐨勫紓甯革細褰揓ava鍐呯疆鐨勫紓甯搁兘涓嶈兘鏄庣‘鐨勮鏄庡紓甯告儏鍐电殑鏃跺€欙紝闇€瑕佸垱寤鸿嚜宸辩殑寮傚父銆?/p>

闇€瑕佹敞鎰忕殑鏄紝鍞竴鏈夌敤鐨勫氨鏄被鍨嬪悕杩欎釜淇℃伅锛屾墍浠ヤ笉瑕佸湪寮傚父绫荤殑璁捐涓婅姳璐圭簿鍔涖€?/p>

3.3 鎹曡幏寮傚父 濡傛灉涓€涓紓甯告病鏈夎澶勭悊锛岄偅涔堬紝瀵逛簬涓€涓潪鍥惧舰鐣岄潰鐨勭▼搴忚€岃█锛岃绋嬪簭浼氳涓骞惰緭鍑哄紓甯镐俊鎭紱瀵逛簬涓€涓浘褰㈢晫闈㈢▼搴忥紝涔熶細杈撳嚭寮傚父鐨勪俊鎭紝浣嗘槸绋嬪簭骞朵笉涓锛岃€屾槸杩斿洖鐢ㄐ紤濞诧紵纭岋紵鍒嗗皬锛烞R> 3.3.1 璇硶锛歵ry銆乧atch鍜?..

Java涓€滃紓甯告満鍒垛€濇槸浠€涔堬紵

銆侊紓寮傚父锛傜殑澶勭悊鏂规硶 鏈変袱绉嶆柟娉曞鐞嗭紓寮傚父锛傦細绗竴绉嶅渚?锛屽皢鍚湁锛傚紓甯革紓鍑哄彛鐨勬柟娉曠洿鎺ユ斁鍒皌y鍧椾腑锛岀劧鍚庣敱绱ч殢鍏跺悗鐨刢atch鍧楁崟鎹夈€?/p>

绗簩绉嶆槸涓嶇洿鎺ョ洃鍚崟鎹夎寮曠敤鏂规硶鐨勶紓寮傚父锛傦紝鑰屾槸灏嗚繖涓紓寮傚父锛傚叧鑱斾紶閫掔粰寮曠敤鏂规硶锛屽悓鏃剁洃鍚崟鎹夊伐浣滀篃鐩稿簲鍚戜笂浼犻€掋€?/p>

渚?: int myMethod2(int dt) { int data = 0; ty{ data = myMethod(dt) }catch(LowZeoException e){ System.out.pintln锛堬紓鍙戠敓鏁版嵁閿欒锛侊紓锛夛紱 e.pintStackTace(); } etun data; } int myMethod(int dt) thows LowZeoException { int data = isLegal(dt)锛?姝ゅ寮曠敤isLegal锛?鏂规硶锛屼絾骞舵病鏈夋崟鎹夊畠鐨勶紓寮傚父锛?etun data锛?} 浠庝笂渚嬩腑鍙互鐪嬪埌鏂规硶myMethod锛?涓庡畠寮曠敤鐨勬柟娉昳sLegal锛?浜х敓鐨勶紓寮傚父锛侺owZeoException寤虹珛浜嗗叧鑱旓紝涔熷氨鏄畬鎴愪簡灏嗭紓寮傚父锛傚叧鑱旂殑鍚戜笂浼犻€掞紝姝ゆ椂鐨刴yMethod锛?鏂规硶浣撲腑铏界劧鍙湁涓€涓猠tun杩斿洖璇彞锛屼絾瀹冧簨瀹炰笂鍚屾牱鏈変袱绉嶆柟寮忕殑鍑芥暟鍑哄彛锛屼竴绉嶆槸鐢眅tun杩斿洖鐨勬暣褰㈠€硷紝鍙︿竴绉嶅垯鏄繑鍥炴柟娉曞悕涓殑thows鍏抽敭瀛楁墍鎸囩殑锛傚紓甯哥被锛傜殑瀹炰緥瀵硅薄銆?/p>

鐩稿簲鐨勶紝鐩戝惉鎹曟崏鐨勫伐浣滀氦缁欎簡涓婁竴灞傛柟娉昺yMethod2锛?銆?/p>

鍚屾牱鐨勯亾鐞嗭紝myMethod2锛?涔熷彲浠ュ皢锛傚紓甯革紓閫氳繃thows鐨勫叧鑱旂户缁悜涓婁紶閫掋€?/p>

杩欐牱鐨勮瘽锛屼竴鏃︿竴涓紓寮傚父锛傝鎹曟崏鍒版椂锛岃繖涓紓寮傚父锛傚繀鏈変竴涓紶閫掕矾寰勶紝鑰屽鏋滄垜浠湪鎹曟崏鐐圭殑catch绋嬪簭鍧椾腑鍔犲叆pintStackTace锛?鏂规硶锛屼究鑳芥竻妤氱殑鐪嬪埌杩欎釜锛傚紓甯革紓鏄€庢牱浼犻€掕繃鏉ョ殑銆?/p>

渚嬪鍦ㄤ緥3濡傛灉鏈夛紓寮傚父锛傝鎹曟崏鍒帮紝e.pintStackTace锛?鎵撳嵃鍑烘潵鐨勭粨鏋滃皢鏄細 LowZeoException: at Example.isLegal at Example myMethod at Example.myMethod2 at Example main 浠庝笂缁撴灉涓垜浠彲浠ョ湅鍒帮紝浠嶭owZeoException锛傚紓甯革紓浜х敓鐐癸紝鍗冲寘鍚玹how new LowZeoException锛?锛涘瓙鍙ョ殑鏂规硶寮€濮嬶紝鐒跺悗涓€鐩磋拷婧埌浜х敓褰撳墠绾跨▼鐨勬柟娉曪紙娉ㄦ剰锛歱intStackTace锛?骞朵笉鏄拷婧埌鎹曟崏鐐圭粨鏉燂紝鑰屾槸鍒颁骇鐢熷綋鍓嶇嚎绋嬬殑鏂规硶缁撴潫锛夈€?/p>

锛傚紓甯革紓浜х敓鐐逛骇鐢熺殑LowZeoException锛傚紓甯革紓瀵硅薄锛岄鍏堣璧嬬粰浜唅sLegal锛?鍏宠仈鐨凩owZeoException绫荤殑鏃犲悕寮曠敤锛岀劧鍚庣户缁祴缁檓yMethod锛?鍏宠仈鐨凩owZeoException绫荤殑鏃犲悕寮曠敤锛屽啀缁х画璧嬬粰myMethod2锛?涓殑catch鍧椾腑鐨勫舰鍙俥锛屾渶鍚庡湪杩欓噷琚鐞嗘帀锛岃繖涓紓寮傚父锛傚璞¢殢鍗虫秷澶便€?/p>

鍙互璇达紝catch锛?{}灏辨槸锛傚紓甯革紓瀵硅薄鐨勭敓鍛界粓缁撶偣銆?/p>

鍙﹀杩樿娉ㄦ剰涓€鐐癸紝鏂规硶涓庯紓寮傚父锛傜殑鍏宠仈鍙互涓€鐩村悜涓婁紶閫掞紝褰撲紶閫掑埌涓巑ain鏂规硶鍏宠仈鍚庯紝鍗冲湪main锛?鏂规硶鐨勫畾涔変腑浣跨敤浜唗hows Exception锛岃繖鏃堕櫎浜嗚櫄鎷熸満娌℃湁鍏跺畠鏂规硶鑳藉寮曠敤main锛?鏂规硶锛屼笖鍦ㄧ▼搴忎腑鍙兘鐪嬩笉鍒皌y鈥atch绋嬪簭鍧楋紝浣嗗苟涓嶄細浜х敓閿欒锛屽洜涓烘鏃惰櫄鎷熸満浼氭崟鎹夛紓寮傚父锛傦紝骞朵笖浼氶粯璁ょ殑璋冪敤pintStackTace锛?鏂规硶鎵撳嵃鍑猴紓寮傚父锛傝矾寰勩€?/p>

鎬讳箣鍙涓€涓柟娉曞叧鑱斾簡锛傚紓甯革紓锛屽彲浠ュ皢杩欎釜锛傚紓甯革紓鍏宠仈鍚戜笂浼犻€掞紝浣嗘槸鏈€缁堝繀椤讳娇鐢╟atch鏉ョ粓姝紓寮傚父锛傦紝鎴栬€呬竴鐩翠紶閫掑埌main锛?鏂规硶浜ょ粰Java铏氭嫙鏈烘潵缁撴潫锛傚紓甯革紓瀵硅薄鐨勭敓鍛斤紝鍚﹀垯鏄€氫笉杩囩紪璇戠殑銆?/p>

鍥涖€佷娇鐢紓寮傚父鏈哄埗锛傜殑闇€瑕佹敞鎰忕殑鍑犵偣 1. 涓€涓柟娉曚腑鍙兘浼氫骇鐢熷绉嶄笉鍚岀殑寮傚父锛屼綘鍙互璁剧疆澶氫釜锛傚紓甯革紓鎶涘嚭鐐规潵瑙e喅杩欎釜闂銆?/p>

2. 锛傚紓甯革紓瀵硅薄浠庝骇鐢熺偣浜х敓鍚庯紝鍒拌鎹曟崏鍚庣粓姝㈢敓鍛界殑鍏ㄨ繃绋嬩腑锛屽疄闄呬笂鏄竴涓紶鍊艰繃绋嬶紝鎵€浠ヤ綘鍙互鏍规嵁闇€瑕侊紝鏉ュ悎鐞嗙殑鎺у埗妫€娴嬪埌锛傚紓甯革紓鐨勭矑搴︺€?/p>

渚嬪鍦ㄤ緥3涓紝濡傛灉浣犲苟涓嶉渶瑕佺煡閬撳叿浣撲骇鐢熺殑鏄疞owZeoException锛傚紓甯革紓锛岄偅涔堜綘鍙互浣跨敤锛傚紓甯革紓鐨勫叕鍏辩埗绫籈xception鏉ョ粨鍚堬紓寮傚父锛傚璞★紝鍗砪atch(Exception e){鈥銆?/p>

鍚屾牱鍦紓寮傚父锛備笌鏂规硶鍏宠仈鐨勪紶閫掕繃绋嬩腑锛屼篃鍙互鏍规嵁闇€瑕佹帶鍒跺叧鑱旓紓寮傚父锛傜殑绮掑害锛屽嵆thows鍚庨潰璺熶笂寮傚父瀵硅薄鐨勭埗绫诲悕銆?/p>

3. 锛傚紓甯告満鍒讹紓涓繕鏈変竴绉嶇壒娈婃儏鍐碘€曗€昍untimeException锛傚紓甯哥被锛傦紙瑙佸浘1锛夛紝杩欎釜锛傚紓甯哥被锛傚拰瀹冪殑鎵€鏈夊瓙绫婚兘鏈変竴涓壒鎬э紝灏辨槸锛傚紓甯革紓瀵硅薄涓€浜х敓灏辫Java铏氭嫙鏈虹洿鎺ュ鐞嗘帀锛屽嵆鍦ㄦ柟娉曚腑鍑虹幇thow 瀛愬彞鐨勫湴鏂逛究琚櫄鎷熸満鎹曟崏浜嗐€?/p>

鍥犳鍑℃槸鎶涘嚭杩欑锛傝繍琛屾椂寮傚父锛傜殑鏂规硶鍦ㄨ寮曠敤鏃讹紝涓嶉渶瑕佹湁ty鈥atch璇彞鏉ュ鐞嗭紓寮傚父锛傘€?/p>

鍦╦ava涓殑寮傚父澶勭悊涓殑寮傚父瀵硅薄鏄粈涔?/h2>

Exception绫讳互鍙婁粬鐨勫瓙绫?鐨勪竴涓疄渚嬪璞℃瘮濡傚父瑙佸紓甯?. java.lang.nullpointerexception 杩欎釜寮傚父澶у鑲畾閮界粡甯搁亣鍒帮紝寮傚父鐨勮В閲婃槸锛傜▼搴忛亣涓婁簡绌烘寚閽堬紓锛岀畝鍗曞湴璇村氨鏄皟鐢ㄤ簡鏈粡鍒濆鍖栫殑瀵硅薄鎴栬€呮槸涓嶅瓨鍦ㄧ殑瀵硅薄锛岃繖涓敊璇粡甯稿嚭鐜板湪鍒涘缓鍥剧墖锛岃皟鐢ㄦ暟缁勮繖浜涙搷浣滀腑锛屾瘮濡傚浘鐗囨湭缁忓垵濮嬪寲锛屾垨鑰呭浘鐗囧垱寤烘椂鐨勮矾寰勯敊璇瓑绛夈€?/p>

瀵规暟缁勬搷浣滀腑鍑虹幇绌烘寚閽堬紝寰堝鎯呭喌涓嬫槸涓€浜涘垰寮€濮嬪涔犵紪绋嬬殑鏈嬪弸甯哥姱鐨勯敊璇紝鍗虫妸鏁扮粍鐨勫垵濮嬪寲鍜屾暟缁勫厓绱犵殑鍒濆鍖栨贩娣嗚捣鏉ヤ簡銆?/p>

鏁扮粍鐨勫垵濮嬪寲鏄鏁扮粍鍒嗛厤闇€瑕佺殑绌洪棿锛岃€屽垵濮嬪寲鍚庣殑鏁扮粍锛屽叾涓殑鍏冪礌骞舵病鏈夊疄渚嬪寲锛屼緷鐒舵槸绌虹殑锛屾墍浠ヨ繕闇€瑕佸姣忎釜鍏冪礌閮借繘琛屽垵濮嬪寲锛堝鏋滆璋冪敤鐨勮瘽锛?2. java.lang.classnotfoundexception 杩欎釜寮傚父鏄緢澶氬師鏈湪jb绛夊紑鍙戠幆澧冧腑寮€鍙戠殑绋嬪簭鍛橈紝鎶妀b涓嬬殑绋嬪簭鍖呮斁鍦╳tk涓嬬紪璇戠粡甯稿嚭鐜扮殑闂锛屽紓甯哥殑瑙i噴鏄紓鎸囧畾鐨勭被涓嶅瓨鍦紓锛岃繖閲屼富瑕佽€冭檻涓€涓嬬被鐨勫悕绉板拰璺緞鏄惁姝g‘鍗冲彲锛屽鏋滄槸鍦╦b涓嬪仛鐨勭▼搴忓寘锛屼竴鑸兘鏄粯璁ゅ姞涓妏ackage鐨勶紝鎵€浠ヨ浆鍒皐tk涓嬪悗瑕佹敞鎰忔妸package鐨勮矾寰勫姞涓娿€?/p>

3. java.lang.arithmeticexception 杩欎釜寮傚父鐨勮В閲婃槸锛傛暟瀛﹁繍绠楀紓甯革紓锛屾瘮濡傜▼搴忎腑鍑虹幇浜嗛櫎浠ラ浂杩欐牱鐨勮繍绠楀氨浼氬嚭杩欐牱鐨勫紓甯革紝瀵硅繖绉嶅紓甯革紝澶у灏辫濂藉ソ妫€鏌ヤ竴涓嬭嚜宸辩▼搴忎腑娑夊強鍒版暟瀛﹁繍绠楃殑鍦版柟锛屽叕寮忔槸涓嶆槸鏈変笉濡ヤ簡銆?/p>

4. java.lang.arrayindexoutofboundsexception 杩欎釜寮傚父鐩镐俊寰堝鏈嬪弸涔熺粡甯搁亣鍒拌繃锛屽紓甯哥殑瑙i噴鏄紓鏁扮粍涓嬫爣瓒婄晫锛傦紝鐜板湪绋嬪簭涓ぇ澶氶兘鏈夊鏁扮粍鐨勬搷浣滐紝鍥犳鍦ㄨ皟鐢ㄦ暟缁勭殑鏃跺€欎竴瀹氳璁ょ湡妫€鏌ワ紝鐪嬭嚜宸辫皟鐢ㄧ殑涓嬫爣鏄笉鏄秴鍑轰簡鏁扮粍鐨勮寖鍥达紝涓€鑸潵璇达紝鏄剧ず锛堝嵆鐩存帴鐢ㄥ父鏁板綋涓嬫爣锛夎皟鐢ㄤ笉澶鏄撳嚭杩欐牱鐨勯敊锛屼絾闅愬紡锛堝嵆鐢ㄥ彉閲忚〃绀轰笅鏍囷級璋冪敤灏辩粡甯稿嚭閿欎簡锛岃繕鏈変竴绉嶆儏鍐碉紝鏄▼搴忎腑瀹氫箟鐨勬暟缁勭殑闀垮害鏄€氳繃鏌愪簺鐗瑰畾鏂规硶鍐冲畾鐨勶紝涓嶆槸浜嬪厛澹版槑鐨勶紝杩欎釜鏃跺€欙紝鏈€濂藉厛鏌ョ湅涓€涓嬫暟缁勭殑length锛屼互鍏嶅嚭鐜拌繖涓紓甯搞€?/p>

5. java.lang.illegalargumentexception 杩欎釜寮傚父鐨勮В閲婃槸锛傛柟娉曠殑鍙傛暟閿欒锛傦紝寰堝j2me鐨勭被搴撲腑鐨勬柟娉曞湪涓€浜涙儏鍐典笅閮戒細寮曞彂杩欐牱鐨勯敊璇紝姣斿闊抽噺璋冭妭鏂规硶涓殑闊抽噺鍙傛暟濡傛灉鍐欐垚璐熸暟灏变細鍑虹幇杩欎釜寮傚父锛屽啀姣斿g.setcolor(int red,int green,int blue)杩欎釜鏂规硶涓殑涓変釜鍊硷紝濡傛灉鏈夎秴杩?55鐨勪篃浼氬嚭鐜拌繖涓紓甯革紝鍥犳涓€鏃﹀彂鐜拌繖涓紓甯革紝鎴戜滑瑕佸仛鐨勶紝灏辨槸璧剁揣鍘绘鏌ヤ竴涓嬫柟娉曡皟鐢ㄤ腑鐨勫弬鏁颁紶閫掓槸涓嶆槸鍑虹幇浜嗛敊璇€?/p>

6. java.lang.illegalaccessexception 杩欎釜寮傚父鐨勮В閲婃槸锛傛病鏈夎闂潈闄愶紓锛屽綋搴旂敤绋嬪簭瑕佽皟鐢ㄤ竴涓被锛屼絾褰撳墠鐨勬柟娉曞嵆娌℃湁瀵硅绫荤殑璁块棶鏉冮檺渚夸細鍑虹幇杩欎釜寮傚父銆?/p>

瀵圭▼搴忎腑鐢ㄤ簡package鐨勬儏鍐典笅瑕佹敞鎰忚繖涓紓甯搞€?/p>

鍏朵粬杩樻湁寰堝寮傚父锛屾垜灏变笉涓€涓€鍒椾妇浜嗭紝鎴戣璇存槑鐨勬槸锛屼竴涓悎鏍肩殑绋嬪簭鍛橈紝闇€瑕佸绋嬪簭涓父瑙佺殑闂鏈夌浉褰撶殑浜嗚В鍜岀浉搴旂殑瑙e喅鍔炴硶锛屽惁鍒欎粎浠呭仠鐣欏湪鍐欑▼搴忚€屼笉浼氭敼绋嬪簭鐨勮瘽锛屼細鏋佸ぇ褰卞搷鍒拌嚜宸辩殑寮€鍙戠殑銆?/p>

鍏充簬寮傚父鐨勫叏閮ㄨ鏄庯紝鍦╝pi閲岄兘鍙互鏌ラ槄銆?/p>

绠楁湳寮傚父绫伙細ArithmeticExecption 绌烘寚閽堝紓甯哥被锛歂ullPointerException 绫诲瀷寮哄埗杞崲寮傚父锛欳lassCastException 鏁扮粍璐熶笅鏍囧紓甯革細NegativeArrayException 鏁扮粍涓嬫爣瓒婄晫寮傚父锛欰rrayIndexOutOfBoundsException 杩濊儗瀹夊叏鍘熷垯寮傚父锛歋ecturityException 鏂囦欢宸茬粨鏉熷紓甯革細EOFException 鏂囦欢鏈壘鍒板紓甯革細FileNotFoundException 瀛楃涓茶浆鎹负鏁板瓧寮傚父锛歂umberFormatException 鎿嶄綔鏁版嵁搴撳紓甯革細SQLException 杈撳叆杈撳嚭寮傚父锛欼OException 鏂规硶鏈壘鍒板紓甯革細NoSuchMethodException java.lang.AbstractMethodError 鎶借薄鏂规硶閿欒銆?/p>

褰撳簲鐢ㄨ瘯鍥捐皟鐢ㄦ娊璞℃柟娉曟椂鎶涘嚭銆?/p>

java.lang.AssertionError 鏂█閿欍€?/p>

鐢ㄦ潵鎸囩ず涓€涓柇瑷€澶辫触鐨勬儏鍐点€?/p>

java.lang.ClassCircularityError 绫诲惊鐜緷璧栭敊璇€?/p>

鍦ㄥ垵濮嬪寲涓€涓被鏃讹紝鑻ユ娴嬪埌绫讳箣闂村惊鐜緷璧栧垯鎶涘嚭璇ュ紓甯搞€?/p>

java.lang.ClassFormatError 绫绘牸寮忛敊璇€?/p>

褰揓ava铏氭嫙鏈鸿瘯鍥句粠涓€涓枃浠朵腑璇诲彇Java绫伙紝鑰屾娴嬪埌璇ユ枃浠剁殑鍐呭涓嶇鍚堢被鐨勬湁鏁堟牸寮忔椂鎶涘嚭銆?/p>

java.lang.Error 閿欒銆?/p>

鏄墍鏈夐敊璇殑鍩虹被锛岀敤浜庢爣璇嗕弗閲嶇殑绋嬪簭杩愯闂銆?/p>

杩欎簺闂閫氬父鎻忚堪涓€浜涗笉搴旇搴旂敤绋嬪簭鎹曡幏鐨勫弽甯告儏鍐点€?/p>

java.lang.ExceptionInInitializerError 鍒濆鍖栫▼搴忛敊璇€?/p>

褰撴墽琛屼竴涓被鐨勯潤鎬佸垵濮嬪寲绋嬪簭鐨勮繃绋嬩腑锛屽彂鐢熶簡寮傚父鏃舵姏鍑恒€?/p>

闈欐€佸垵濮嬪寲绋嬪簭鏄寚鐩存帴鍖呭惈浜庣被涓殑static璇彞娈点€?/p>

java.lang.IllegalAccessError 杩濇硶璁块棶閿欒銆?/p>

褰撲竴涓簲鐢ㄨ瘯鍥捐闂€佷慨鏀规煇涓被鐨勫煙锛團ield锛夋垨鑰呰皟鐢ㄥ叾鏂规硶锛屼絾鏄張杩濆弽鍩熸垨鏂规硶鐨勫彲瑙佹€у0鏄庯紝鍒欐姏鍑鸿寮傚父銆?/p>

java.lang.IncompatibleClassChangeError 涓嶅吋瀹圭殑绫诲彉鍖栭敊璇€?/p>

褰撴鍦ㄦ墽琛岀殑鏂规硶鎵€渚濊禆鐨勭被瀹氫箟鍙戠敓浜嗕笉鍏煎鐨勬敼鍙樻椂锛屾姏鍑鸿寮傚父銆?/p>

涓€鑸湪淇敼浜嗗簲鐢ㄤ腑鐨勬煇浜涚被鐨勫0鏄庡畾涔夎€屾病鏈夊鏁翠釜搴旂敤閲嶆柊缂栬瘧鑰岀洿鎺ヨ繍琛岀殑鎯呭喌涓嬶紝瀹规槗寮曞彂璇ラ敊璇€?/p>

java.lang.InstantiationError 瀹炰緥鍖栭敊璇€?/p>

褰撲竴涓簲鐢ㄨ瘯鍥鹃€氳繃Java鐨刵ew鎿嶄綔绗︽瀯閫犱竴涓娊璞$被鎴栬€呮帴鍙f椂鎶涘嚭璇ュ紓甯? java.lang.InternalError 鍐呴儴閿欒銆?/p>

鐢ㄤ簬鎸囩ずJava铏氭嫙鏈哄彂鐢熶簡鍐呴儴閿欒銆?/p>

java.lang.LinkageError 閾炬帴閿欒銆?/p>

璇ラ敊璇強鍏舵墍鏈夊瓙绫绘寚绀烘煇涓被渚濊禆浜庡彟澶栦竴浜涚被锛屽湪璇ョ被缂栬瘧涔嬪悗锛岃渚濊禆鐨勭被鏀瑰彉浜嗗叾绫诲畾涔夎€屾病鏈夐噸鏂扮紪璇戞墍鏈夌殑绫伙紝杩涜€屽紩鍙戦敊璇殑鎯呭喌銆?/p>

java.lang....

Java鐨勫紓甯稿鐞嗘湁鍝簺鏂规硶鍛紵

1.寮傚父澶勭悊锛?鍦↗ava璇█鐨勯敊璇鐞嗙粨鏋勭敱try,catch,finally涓変釜鍧楃粍鎴愩€?/p>

鍏朵腑try鍧楀瓨鏀惧皢鍙兘鍙戠敓寮傚父鐨凧ava璇█锛屽苟绠$悊鐩稿叧鐨勫紓甯告寚閽堬紱catch鍧楃揣璺熷湪try鍧楀悗闈紝鐢ㄦ潵婵€鍙戣鎹曡幏鐨勫紓甯革紱finally鍧楀寘鍚竻闄ょ▼搴忔病鏈夐噴鏀剧殑璧勬簮锛屽彞鏌勭瓑銆?/p>

涓嶇try鍧椾腑鐨勪唬鐮佸浣曢€€鍑猴紝閮藉皢鎵цfinally鍧椼€?/p>

2.鎶涘嚭寮傚父锛?Java璇█鍙互涓嶅湪鏂规硶涓洿鎺ユ崟鑾凤紝鑰岀敤throw璇彞灏嗗紓甯告姏缁欎笂灞傜殑璋冪敤鑰呫€?/p>

Throw璇彞灏辨槸鏉ユ槑纭湴鎶涘嚭涓€涓紓甯革紱棣栧厛浣犲繀闇€寰楀埌涓€涓猅hrowable鐨勫疄渚嬪彞鏌勶紝閫氳繃鍙傛暟浼犲埌catch涓紝鎴栬€呴噰鐢╪ew鎿嶄綔绗︽潵鍒涘缓涓€涓€?/p>

渚嬪锛歱ublic void execute(String str,int index) throws WhcaException{ try{ //鍙兘浜х敓寮傚父鐨勪唬鐮侊紱 }catch(Exception e){ throw new WhcaException(鈥淛B: M:鈥?e.getMessage)锛?} } 鏈€鍚庤皥璋堜娇鐢紓寮傚父鏈哄埗锛傜殑闇€瑕佹敞鎰忕殑鍑犵偣 1.涓€涓柟娉曚腑鍙兘浼氫骇鐢熷绉嶄笉鍚岀殑寮傚父锛屼綘鍙互璁剧疆澶氫釜锛傚紓甯革紓鎶涘嚭鐐规潵瑙e喅杩欎釜闂銆?/p>

2.锛傚紓甯革紓瀵硅薄浠庝骇鐢熺偣浜х敓鍚庯紝鍒拌鎹曟崏鍚庣粓姝㈢敓鍛界殑鍏ㄨ繃绋嬩腑锛屽疄闄呬笂鏄竴涓紶鍊艰繃绋嬶紝鎵€浠ヤ綘鍙互鏍规嵁闇€瑕侊紝鏉ュ悎鐞嗙殑鎺у埗妫€娴嬪埌锛傚紓甯革紓鐨勭矑搴︺€?/p>

3.锛傚紓甯告満鍒讹紓涓繕鏈変竴绉嶇壒娈婃儏鍐碘€曗€昍untimeException锛傚紓甯哥被锛傦紝杩欎釜锛傚紓甯哥被锛傚拰瀹冪殑鎵€鏈夊瓙绫婚兘鏈変竴涓壒鎬э紝灏辨槸锛傚紓甯革紓瀵硅薄涓€浜х敓灏辫Java铏氭嫙鏈虹洿鎺ュ鐞嗘帀锛屽嵆鍦ㄦ柟娉曚腑鍑虹幇throw瀛愬彞鐨勫湴鏂逛究琚櫄鎷熸満鎹曟崏浜嗐€?/p>

鍥犳鍑℃槸鎶涘嚭杩欑锛傝繍琛屾椂寮傚父锛傜殑鏂规硶鍦ㄨ寮曠敤鏃讹紝涓嶉渶瑕佹湁try鈥atch璇彞鏉ュ鐞嗭紓寮傚父锛傘€?/p>

4.鎹曡幏浜嗗紓甯革紝灏辫瀵瑰畠杩涜閫傚綋鐨勫鐞嗐€?/p>

璋冪敤printStackTrace瀵硅皟璇曠▼搴忔湁甯姪锛屼絾娌℃湁鐪熸鐨勫鐞嗗紓甯革紒 printStackTrace锛?鏂规硶鍙兘鍒嗘瀽绋嬪簭杩愯鎯呭喌锛屽湪JDK1.4涓彁渚涗簡StackTraceElement绫绘潵鑾峰彇绋嬪簭鐨勮繍琛屾儏鍐碉紒渚嬶細 import javax.swing.*; public class StackTraceTest { public static int factorial(int n) { System.out.print锛堬紓杩唬锛?锛堬紓+n+锛傦級:锛?; Throwable t=new Throwable(); StackTraceElement[] frames=t.getStackTrace(); for(int i=0;i缁熻浣犲啓鐨刯ava浠g爜琛屾暟

Java涓轰粈涔堣寮曞叆寮傚父澶勭悊鏈哄埗锛?/h2>

铏界劧鏈夌偣澶?浣嗘槸瀵逛綘鏈夌敤 濂藉ソ鍘荤湅鐪嬪惂寮傚父澶勭悊鏄▼搴忚璁′腑涓€涓潪甯搁噸瑕佺殑鏂归潰锛屼篃鏄▼搴忚璁$殑涓€澶ч毦鐐癸紝浠嶤寮€濮嬶紝浣犱篃璁稿凡缁忕煡閬撳浣曠敤if...else...鏉ユ帶鍒跺紓甯镐簡锛屼篃璁告槸鑷彂鐨勶紝鐒惰€岃繖绉嶆帶鍒跺紓甯哥棝鑻︼紝鍚屼竴涓紓甯告垨鑰呴敊璇鏋滃涓湴鏂瑰嚭鐜帮紝閭d箞浣犳瘡涓湴鏂归兘瑕佸仛鐩稿悓澶勭悊锛屾劅瑙夌浉褰撶殑楹荤儲锛?Java璇█鍦ㄨ璁$殑褰撳垵灏辫€冭檻鍒拌繖浜涢棶棰橈紝鎻愬嚭寮傚父澶勭悊鐨勬鏋剁殑鏂规锛屾墍鏈夌殑寮傚父閮藉彲浠ョ敤涓€涓被鍨嬫潵琛ㄧず锛屼笉鍚岀被鍨嬬殑寮傚父瀵瑰簲涓嶅悓鐨勫瓙绫诲紓甯革紙杩欓噷鐨勫紓甯稿寘鎷敊璇蹇碉級锛屽畾涔夊紓甯稿鐞嗙殑瑙勮寖锛屽湪1.4鐗堟湰浠ュ悗澧炲姞浜嗗紓甯搁摼鏈哄埗锛屼粠鑰屼究浜庤窡韪紓甯革紒杩欐槸Java璇█璁捐鑰呯殑楂樻槑涔嬪锛屼篃鏄疛ava璇█涓殑涓€涓毦鐐癸紝涓嬮潰鏄垜瀵笿ava寮傚父鐭ヨ瘑鐨勪竴涓€荤粨锛屼篃绠楁槸璧勬簮鍥炴敹涓€涓嬨€?/p>

涓€銆丣ava寮傚父鐨勫熀纭€鐭ヨ瘑 寮傚父鏄▼搴忎腑鐨勪竴浜涢敊璇紝浣嗗苟涓嶆槸鎵€鏈夌殑閿欒閮芥槸寮傚父锛屽苟涓旈敊璇湁鏃跺€欐槸鍙互閬垮厤鐨勩€?/p>

姣斿璇达紝浣犵殑浠g爜灏戜簡涓€涓垎鍙凤紝閭d箞杩愯鍑烘潵缁撴灉鏄彁绀烘槸閿欒java.lang.Error锛涘鏋滀綘鐢⊿ystem.out.println(11/0)锛岄偅涔堜綘鏄洜涓轰綘鐢?鍋氫簡闄ゆ暟锛屼細鎶涘嚭java.lang.ArithmeticException鐨勫紓甯搞€?/p>

鏈変簺寮傚父闇€瑕佸仛澶勭悊锛屾湁浜涘垯涓嶉渶瑕佹崟鑾峰鐞嗭紝鍚庨潰浼氳缁嗚鍒般€?/p>

澶╂湁涓嶆祴椋庝簯锛屼汉鏈夋棪澶曠ジ绂忥紝Java鐨勭▼搴忎唬鐮佷篃濡傛銆?/p>

鍦ㄧ紪绋嬭繃绋嬩腑锛岄鍏堝簲褰撳敖鍙兘鍘婚伩鍏嶉敊璇拰寮傚父鍙戠敓锛屽浜庝笉鍙伩鍏嶃€佷笉鍙娴嬬殑鎯呭喌鍒欏湪鑰冭檻寮傚父鍙戠敓鏃跺浣曞鐞嗐€?/p>

Java涓殑寮傚父鐢ㄥ璞℃潵琛ㄧず銆?/p>

Java瀵瑰紓甯哥殑澶勭悊鏄寜寮傚父鍒嗙被澶勭悊鐨勶紝涓嶅悓寮傚父鏈変笉鍚岀殑鍒嗙被锛屾瘡绉嶅紓甯搁兘瀵瑰簲涓€涓被鍨嬶紙class锛夛紝姣忎釜寮傚父閮藉搴斾竴涓紓甯革紙绫荤殑锛夊璞°€?/p>

寮傚父绫讳粠鍝噷鏉ワ紵鏈変袱涓潵婧愶紝涓€鏄疛ava璇█鏈韩瀹氫箟鐨勪竴浜涘熀鏈紓甯哥被鍨嬶紝浜屾槸鐢ㄦ埛閫氳繃缁ф壙Exception绫绘垨鑰呭叾瀛愮被鑷繁瀹氫箟鐨勫紓甯搞€?/p>

Exception 绫诲強鍏跺瓙绫绘槸 Throwable 鐨勪竴绉嶅舰寮忥紝瀹冩寚鍑轰簡鍚堢悊鐨勫簲鐢ㄧ▼搴忔兂瑕佹崟鑾风殑鏉′欢銆?/p>

寮傚父鐨勫璞′粠鍝噷鏉ュ憿锛熸湁涓や釜鏉ユ簮锛屼竴鏄疛ava杩愯鏃剁幆澧冭嚜鍔ㄦ姏鍑虹郴缁熺敓鎴愮殑寮傚父锛岃€屼笉绠′綘鏄惁鎰挎剰鎹曡幏鍜屽鐞嗭紝瀹冩€昏琚姏鍑猴紒姣斿闄ゆ暟涓?鐨勫紓甯搞€?/p>

浜屾槸绋嬪簭鍛樿嚜宸辨姏鍑虹殑寮傚父锛岃繖涓紓甯稿彲浠ユ槸绋嬪簭鍛樿嚜宸卞畾涔夌殑锛屼篃鍙互鏄疛ava璇█涓畾涔夌殑锛岀敤throw 鍏抽敭瀛楁姏鍑哄紓甯革紝杩欑寮傚父甯哥敤鏉ュ悜璋冪敤鑰呮眹鎶ュ紓甯哥殑涓€浜涗俊鎭€?/p>

寮傚父鏄拡瀵规柟娉曟潵璇寸殑锛屾姏鍑恒€佸0鏄庢姏鍑恒€佹崟鑾峰拰澶勭悊寮傚父閮芥槸鍦ㄦ柟娉曚腑杩涜鐨勩€?/p>

Java寮傚父澶勭悊閫氳繃5涓叧閿瓧try銆乧atch銆乼hrow銆乼hrows銆乫inally杩涜绠$悊銆?/p>

鍩烘湰杩囩▼鏄敤try璇彞鍧楀寘浣忚鐩戣鐨勮鍙ワ紝濡傛灉鍦╰ry璇彞鍧楀唴鍑虹幇寮傚父锛屽垯寮傚父浼氳鎶涘嚭锛屼綘鐨勪唬鐮佸湪catch璇彞鍧椾腑鍙互鎹曡幏鍒拌繖涓紓甯稿苟鍋氬鐞嗭紱杩樻湁浠ラ儴鍒嗙郴缁熺敓鎴愮殑寮傚父鍦↗ava杩愯鏃惰嚜鍔ㄦ姏鍑恒€?/p>

浣犱篃鍙互閫氳繃throws鍏抽敭瀛楀湪鏂规硶涓婂0鏄庤鏂规硶瑕佹姏鍑哄紓甯革紝鐒跺悗鍦ㄦ柟娉曞唴閮ㄩ€氳繃throw鎶涘嚭寮傚父瀵硅薄銆?/p>

finally璇彞鍧椾細鍦ㄦ柟娉曟墽琛宺eturn涔嬪墠鎵ц锛屼竴鑸粨鏋勫涓嬶細 try{ 绋嬪簭浠g爜 }catch锛堝紓甯哥被鍨? 寮傚父鐨勫彉閲忓悕1锛墈 绋嬪簭浠g爜 }catch锛堝紓甯哥被鍨? 寮傚父鐨勫彉閲忓悕2锛墈 绋嬪簭浠g爜 }finally{ 绋嬪簭浠g爜 } catch璇彞鍙互鏈夊涓紝鐢ㄦ潵鍖归厤澶氫釜寮傚父锛屽尮閰嶄笂澶氫釜涓竴涓悗锛屾墽琛宑atch璇彞鍧楁椂鍊欎粎浠呮墽琛屽尮閰嶄笂鐨勫紓甯搞€?/p>

catch鐨勭被鍨嬫槸Java璇█涓畾涔夌殑鎴栬€呯▼搴忓憳鑷繁瀹氫箟鐨勶紝琛ㄧず浠g爜鎶涘嚭寮傚父鐨勭被鍨嬶紝寮傚父鐨勫彉閲忓悕琛ㄧず鎶涘嚭寮傚父鐨勫璞$殑寮曠敤锛屽鏋渃atch鎹曡幏骞跺尮閰嶄笂浜嗚寮傚父锛岄偅涔堝氨鍙互鐩存帴鐢ㄨ繖涓紓甯稿彉閲忓悕锛屾鏃惰寮傚父鍙橀噺鍚嶆寚鍚戞墍鍖归厤鐨勫紓甯革紝骞朵笖鍦╟atch浠g爜鍧椾腑鍙互鐩存帴寮曠敤銆?/p>

杩欎竴鐐归潪甯搁潪甯哥殑鐗规畩鍜岄噸瑕侊紒 Java寮傚父澶勭悊鐨勭洰鐨勬槸鎻愰珮绋嬪簭鐨勫仴澹€э紝浣犲彲浠ュ湪catch鍜宖inally浠g爜鍧椾腑缁欑▼搴忎竴涓慨姝f満浼氾紝浣垮緱绋嬪簭涓嶅洜寮傚父鑰岀粓姝㈡垨鑰呮祦绋嬪彂鐢熶互澶栫殑鏀瑰彉銆?/p>

鍚屾椂锛岄€氳繃鑾峰彇Java寮傚父淇℃伅锛屼篃涓虹▼搴忕殑寮€鍙戠淮鎶ゆ彁渚涗簡鏂逛究锛屼竴鑸€氳繃寮傚父淇℃伅灏卞緢蹇氨鑳芥壘鍒板嚭鐜板紓甯哥殑闂锛堜唬鐮侊級鎵€鍦ㄣ€?/p>

Java寮傚父澶勭悊鏄疛ava璇█鐨勪竴澶х壒鑹诧紝涔熸槸涓毦鐐癸紝鎺屾彙寮傚父澶勭悊鍙互璁╁啓鐨勪唬鐮佹洿鍋ュ.鍜屾槗浜庣淮鎶ゃ€?/p>

浜屻€丣ava寮傚父绫荤被鍥?涓嬮潰鏄繖鍑犱釜绫荤殑灞傛鍥撅細 java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.Error java.lang.ThreadDeath 涓嬮潰鍥涗釜绫荤殑浠嬬粛鏉ヨ嚜java api 鏂囨。銆?/p>

1銆乀hrowable Throwable 绫绘槸 Java 璇█涓墍鏈夐敊璇垨寮傚父鐨勮秴绫汇€?/p>

鍙湁褰撳璞℃槸姝ょ被锛堟垨鍏跺瓙绫讳箣涓€锛夌殑瀹炰緥鏃讹紝鎵嶈兘閫氳繃 Java 铏氭嫙鏈烘垨鑰?Java throw 璇彞鎶涘嚭銆?/p>

绫讳技鍦帮紝鍙湁姝ょ被鎴栧叾瀛愮被涔嬩竴鎵嶅彲浠ユ槸 catch 瀛愬彞涓殑鍙傛暟绫诲瀷銆?/p>

涓や釜瀛愮被鐨勫疄渚嬶紝Error 鍜?Exception锛岄€氬父鐢ㄤ簬鎸囩ず鍙戠敓浜嗗紓甯告儏鍐点€?/p>

閫氬父锛岃繖浜涘疄渚嬫槸鍦ㄥ紓甯告儏鍐电殑涓婁笅鏂囦腑鏂拌繎鍒涘缓鐨勶紝鍥犳鍖呭惈浜嗙浉鍏崇殑淇℃伅锛堟瘮濡傚爢鏍堣窡韪暟鎹級銆?/p>

2銆丒xception Exception 绫诲強鍏跺瓙绫绘槸 Throwable 鐨勪竴绉嶅舰寮忥紝瀹冩寚鍑轰簡鍚堢悊鐨勫簲鐢ㄧ▼搴忔兂瑕佹崟鑾风殑鏉′欢锛岃〃绀虹▼搴忔湰韬彲浠ュ鐞嗙殑寮傚父銆?/p>

3銆丒rror Error 鏄?Throwable 鐨勫瓙绫伙紝琛ㄧず浠呴潬绋嬪簭鏈韩鏃犳硶鎭㈠鐨勪弗閲嶉敊璇紝鐢ㄤ簬鎸囩ず鍚堢悊鐨勫簲鐢ㄧ▼搴忎笉搴旇璇曞浘鎹曡幏鐨勪弗閲嶉棶棰樸€?/p>

鍦ㄦ墽琛岃鏂规硶鏈熼棿锛屾棤闇€鍦ㄦ柟娉曚腑閫氳繃throws澹版槑鍙兘鎶涘嚭浣嗘病鏈夋崟鑾风殑 Error 鐨勪换浣曞瓙绫伙紝鍥犱负Java缂栬瘧鍣ㄤ笉鍘绘鏌ュ畠锛屼篃灏辨槸璇达紝褰撶▼搴忎腑鍙兘鍑虹幇杩欑被寮傚父鏃讹紝鍗充娇娌℃湁鐢╰ry...catch璇彞鎹曡幏瀹冿紝涔熸病鏈夌敤throws瀛楀彞澹版槑鎶涘嚭...

JAVA涓殑寮傚父澶勭悊鏈哄埗鐨勫師鐞?/h2>

Java涓殑寮傚父澶勭悊鏈哄埗鐨勭畝鍗曞師鐞嗗拰搴旂敤 锛?java涓璗hrowable杩欎釜绫诲彲浠ヨ浣滀负寮傚父鎶涘嚭鐨勭被锛岀户鎵垮畠鐨勫垎涓哄紓甯窫xception鍜岄敊璇疎rror.Exception琛ㄧず绋嬪簭闇€瑕佹崟鎹夊拰澶勭悊鐨勭殑寮傚父锛汦rror琛ㄧず绯荤粺绾у埆鐨勯敊璇拰绋嬪簭鏃犻渶澶勭悊鐨勩€?/p>

鎴戜滑鎵€闇€瑕佸叧蹇冪殑鏄疎xception. Execption鍙互鍒嗕负java鏍囧噯瀹氫箟鐨勫紓甯稿拰绋嬪簭鍛樿嚜瀹氫箟寮傚父2绉? 锛?锛変竴绉嶆槸褰撶▼搴忚繚鍙嶄簡java璇鍒欑殑鏃跺€欙紝JAVA铏氭嫙鏈哄氨浼氬皢鍙戠敓鐨勯敊璇〃绀轰负涓€涓紓甯?杩欓噷璇硶瑙勫垯鎸囩殑鏄疛AVA绫诲簱鍐呯疆鐨勮涔夋鏌ャ€?/p>

锛?锛夊彟涓€绉嶆儏鍐靛氨鏄疛AVA鍏佽绋嬪簭鍛樻墿灞曡繖绉嶈涔夋鏌ワ紝绋嬪簭鍛樺彲浠ュ垱寤鸿嚜宸辩殑寮傚父锛屽苟鑷敱閫夋嫨鍦ㄤ綍鏃剁敤throw鍏抽敭瀛楀紩鍙戝紓甯搞€?/p>

鎵€鏈夌殑寮傚父閮芥槸Thowable鐨勫瓙绫汇€?/p>

寮傚父澶勭悊鏄笌绋嬪簭鎵ц鏄苟琛岀殑.Try{ //鍙兘鍙戠幇寮傚父鐨勮鍙ュ潡}catch锛堝紓甯哥被鍨嬶紝e锛墈 //鍙戠敓寮傚父鏃跺€欑殑鎵ц璇彞鍧梷 finnally{ //涓嶇鏄惁鍙戠敓寮傚父閮芥墽琛岀殑璇彞鍧梷浠ヤ笅鏄竴涓嚜瀹氫箟寮傚父娴嬭瘯绫伙細package code;class MyException extends Exception{ public void f() { System.out.println(锛倀his is my Exception!!锛?; }}public class ExceptionTestTwo { private int i = 0; private int j; ExceptionTestTwo(int x) throws MyException { f2(); j = x / i; } public void f2() throws MyException { System.out.println(锛倀his is My first Exception!!锛?; throw new MyException(); } public static void main(String[] args) { try { new ExceptionTestTwo(9); } catch (MyException e2) { e2.f(); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(锛侳inally is first Exception!!锛?; } try { throw new MyException(); } catch (MyException e1) { e1.f(); } finally { System.out.println(锛侳inally is second Exception!!锛?; } }}

java涓叧浜庡紓甯稿浣曞鐞嗭紵

//闄ら浂寮傚父锛屼笉鍋氬鐞?class test{ int devide(int x,int y) {return x/y;} } class TestException{ public static void main(String arg[]) { new test().devide(3,0); System.out.println(锛倀he end锛侊紒锛?锛?} } //闄ら浂寮傚父锛岃繘琛屾崟鑾?class test{ int devide(int x,int y) {return x/y;} } class CatchException{ public static void main(String arg[]) { try{ new test().devide(3,0); } catch(ArithmeticException l)//exception绫?澹板悕 瀵硅薄l {//catch涓彲浠ユ墽琛屽叾浠栬鍙ヨ緭鍑?// l.printStackTrace() ;//printStackTrace // public void printStackTrace(PrintWriter s) // 灏嗘 throwable 鍙婂叾杩借釜杈撳嚭鍒版寚瀹氱殑 PrintWriter銆?/p>

Java寮傚父浠ュ強鎬庢牱澶勭悊寮傚父锛?

Java 寮傚父浠ュ強寮傚父澶勭悊 鍙皢 Java 寮傚父鐪嬩綔鏄竴绫绘秷鎭紝瀹冧紶閫佷竴浜涚郴缁熼棶棰樸€佹晠闅滃強鏈寜瑙勫畾鎵ц鐨勫姩浣滅殑鐩稿叧淇℃伅銆?/p>

寮傚父鍖呭惈淇℃伅锛屼互灏嗕俊鎭粠搴旂敤绋嬪簭鐨勪竴閮ㄥ垎鍙戦€佸埌鍙︿竴閮ㄥ垎銆?/p>

缂栬瘧璇█涓轰綍瑕佸鐞嗗紓甯革紵涓轰綍涓嶅湪寮傚父鍑虹幇浣嶇疆闅忔椂澶勭悊鍏蜂綋鏁呴殰锛熷洜涓烘湁鏃跺€欐垜浠渶瑕佸湪绯荤粺涓氦娴侀敊璇秷鎭紝浠ヤ究鎸夌収缁熶竴鐨勬柟寮忓鐞嗛棶棰橈紝鏈夋椂鏄洜涓烘湁鑻ュ共澶勭悊闂鐨勫彲鑳芥柟寮忥紝浣嗘偍涓嶇煡閬撲娇鐢ㄥ摢涓€绉嶏紝姝ゆ椂锛屽彲灏嗗鐞嗗紓甯哥殑浠诲姟濮旀墭缁欒皟鐢ㄦ柟娉曠殑浠g爜銆?/p>

璋冪敤鑰呴€氬父鏇磋兘浜嗚В闂鏉ユ簮鐨勪笂涓嬫枃锛岃兘鏇村ソ鐨勭‘瀹氭仮澶嶆柟寮忋€?/p>

蹇呭畾鍦ㄨ繍琛岀殑 Java 搴旂敤绋嬪簭鐨勪竴浜涚被鎴栧璞′腑浜х敓寮傚父銆?/p>

鍑虹幇鏁呴殰鏃讹紝鈥滃彂閫佽€呪€濆皢浜х敓寮傚父瀵硅薄銆?/p>

寮傚父鍙兘浠h〃 Java 浠g爜鍑虹幇鐨勯棶棰橈紝涔熷彲鑳芥槸 JVM 鐨勭浉搴旈敊璇紝鎴栧熀纭€纭欢鎴栨搷浣滅郴缁熺殑閿欒銆?/p>

寮傚父鏈韩琛ㄧず娑堟伅锛屾寚鍙戦€佽€呬紶缁欐帴鏀惰€呯殑鏁版嵁鈥滆礋鑽封€濄€?/p>

棣栧厛锛屽紓甯稿熀浜庣被鐨勭被鍨嬫潵浼犺緭鏈夌敤淇℃伅銆?/p>

寰堝鎯呭喌涓嬶紝鍩轰簬寮傚父鐨勭被鏃㈣兘璇嗗埆鏁呴殰鏈洜骞惰兘鏇存闂銆?/p>

鍏舵锛屽紓甯歌繕甯︽湁鍙兘鏈夌敤鐨勬暟鎹紙濡傚睘鎬э級銆?/p>

鍦ㄥ鐞嗗紓甯告椂锛屾秷鎭繀椤绘湁鎺ユ敹鑰咃紱鍚﹀垯灏嗘棤娉曞鐞嗕骇鐢熷紓甯哥殑搴曞眰闂銆?/p>

鍦ㄤ笂渚嬩腑锛屽紓甯糕€滀骇鐢熻€呪€濇槸璇诲彇鏂囨湰琛岀殑 BufferedReader銆?/p>

鍦ㄦ晠闅滃嚭鐜版椂锛屽皢鍦?readLine 鏂规硶涓瀯寤?IOException 瀵硅薄銆?/p>

寮傚父鈥滄帴鏀惰€呪€濇槸浠g爜鏈韩銆?/p>

EchoInputHandle 搴旂敤绋嬪簭鐨?try-catch 缁撴瀯涓殑 catch 鍧楁槸寮傚父鐨勬帴鏀惰€咃紝瀹冧互瀛楃涓插舰寮忚緭鍑哄紓甯革紝灏嗛棶棰樿褰曚笅鏉ャ€?/p>

Java 寮傚父绫荤殑灞傛缁撴瀯 鍦ㄦ垜浠粠鎬讳綋涓婁簡瑙e紓甯稿悗锛屾垜浠簲璇ヤ簡瑙e浣曞湪 Java 搴旂敤绋嬪簭涓娇鐢ㄥ紓甯革紝鍗抽渶瑕佷簡瑙?Java 绫荤殑灞傛缁撴瀯銆?/p>

蹇呭畾鍦ㄨ繍琛岀殑 Java 搴旂敤绋嬪簭鐨勪竴浜涚被鎴栧璞′腑浜х敓寮傚父銆?/p>

鍑虹幇鏁呴殰鏃讹紝鈥滃彂閫佽€呪€濆皢浜х敓寮傚父瀵硅薄銆?/p>

寮傚父鍙兘浠h〃 Java 浠g爜鍑虹幇鐨勯棶棰橈紝涔熷彲鑳芥槸 JVM 鐨勭浉搴旈敊璇紝鎴栧熀纭€纭欢鎴栨搷浣滅郴缁熺殑閿欒銆?/p>

寮傚父鏈韩琛ㄧず娑堟伅锛屾寚鍙戦€佽€呬紶缁欐帴鏀惰€呯殑鏁版嵁鈥滆礋鑽封€濄€?/p>

棣栧厛锛屽紓甯稿熀浜庣被鐨勭被鍨嬫潵浼犺緭鏈夌敤淇℃伅銆?/p>

寰堝鎯呭喌涓嬶紝鍩轰簬寮傚父鐨勭被鏃㈣兘璇嗗埆鏁呴殰鏈洜骞惰兘鏇存闂銆?/p>

鍏舵锛屽紓甯歌繕甯︽湁鍙兘鏈夌敤鐨勬暟鎹紙濡傚睘鎬э級銆?/p>

鍦ㄥ鐞嗗紓甯告椂锛屾秷鎭繀椤绘湁鎺ユ敹鑰咃紱鍚﹀垯灏嗘棤娉曞鐞嗕骇鐢熷紓甯哥殑搴曞眰闂銆?/p>

鍦ㄤ笂渚嬩腑锛屽紓甯糕€滀骇鐢熻€呪€濇槸璇诲彇鏂囨湰琛岀殑 BufferedReader銆?/p>

鍦ㄦ晠闅滃嚭鐜版椂锛屽皢鍦?readLine 鏂规硶涓瀯寤?IOException 瀵硅薄銆?/p>

寮傚父鈥滄帴鏀惰€呪€濇槸浠g爜鏈韩銆?/p>

EchoInputHandle 搴旂敤绋嬪簭鐨?try-catch 缁撴瀯涓殑 catch 鍧楁槸寮傚父鐨勬帴鏀惰€咃紝瀹冧互瀛楃涓插舰寮忚緭鍑哄紓甯革紝灏嗛棶棰樿褰曚笅鏉ャ€?/p>

Java 寮傚父绫荤殑灞傛缁撴瀯 鍦ㄦ垜浠粠鎬讳綋涓婁簡瑙e紓甯稿悗锛屾垜浠簲璇ヤ簡瑙e浣曞湪 Java 搴旂敤绋嬪簭涓娇鐢ㄥ紓甯革紝鍗抽渶瑕佷簡瑙?Java 绫荤殑灞傛缁撴瀯銆?/p>

蹇呭畾鍦ㄨ繍琛岀殑 Java 搴旂敤绋嬪簭鐨勪竴浜涚被鎴栧璞′腑浜х敓寮傚父銆?/p>

鍑虹幇鏁呴殰鏃讹紝鈥滃彂閫佽€呪€濆皢浜х敓寮傚父瀵硅薄銆?/p>

寮傚父鍙兘浠h〃 Java 浠g爜鍑虹幇鐨勯棶棰橈紝涔熷彲鑳芥槸 JVM 鐨勭浉搴旈敊璇紝鎴栧熀纭€纭欢鎴栨搷浣滅郴缁熺殑閿欒銆?/p>

寮傚父鏈韩琛ㄧず娑堟伅锛屾寚鍙戦€佽€呬紶缁欐帴鏀惰€呯殑鏁版嵁鈥滆礋鑽封€濄€?/p>

棣栧厛锛屽紓甯稿熀浜庣被鐨勭被鍨嬫潵浼犺緭鏈夌敤淇℃伅銆?/p>

寰堝鎯呭喌涓嬶紝鍩轰簬寮傚父鐨勭被鏃㈣兘璇嗗埆鏁呴殰鏈洜骞惰兘鏇存闂銆?/p>

鍏舵锛屽紓甯歌繕甯︽湁鍙兘鏈夌敤鐨勬暟鎹紙濡傚睘鎬э級銆?/p>

鍦ㄥ鐞嗗紓甯告椂锛屾秷鎭繀椤绘湁鎺ユ敹鑰咃紱鍚﹀垯灏嗘棤娉曞鐞嗕骇鐢熷紓甯哥殑搴曞眰闂銆?/p>

鍦ㄤ笂渚嬩腑锛屽紓甯糕€滀骇鐢熻€呪€濇槸璇诲彇鏂囨湰琛岀殑 BufferedReader銆?/p>

鍦ㄦ晠闅滃嚭鐜版椂锛屽皢鍦?readLine 鏂规硶涓瀯寤?IOException 瀵硅薄銆?/p>

寮傚父鈥滄帴鏀惰€呪€濇槸浠g爜鏈韩銆?/p>

EchoInputHandle 搴旂敤绋嬪簭鐨?try-catch 缁撴瀯涓殑 catch 鍧楁槸寮傚父鐨勬帴鏀惰€咃紝瀹冧互瀛楃涓插舰寮忚緭鍑哄紓甯革紝灏嗛棶棰樿褰曚笅鏉ャ€?/p>

Java 寮傚父绫荤殑灞傛缁撴瀯 鍦ㄦ垜浠粠鎬讳綋涓婁簡瑙e紓甯稿悗锛屾垜浠簲璇ヤ簡瑙e浣曞湪 Java 搴旂敤绋嬪簭涓娇鐢ㄥ紓甯革紝鍗抽渶瑕佷簡瑙?Java 绫荤殑灞傛缁撴瀯銆?/p>

鍦?Java 涓紝鎵€鏈夌殑寮傚父閮芥湁涓€涓叡鍚岀殑绁栧厛 Throwable锛堝彲鎶涘嚭锛夈€?/p>

Throwable 鎸囧畾浠g爜涓彲鐢ㄥ紓甯镐紶鎾満鍒堕€氳繃 Java 搴旂敤绋嬪簭浼犺緭鐨勪换浣曢棶棰樼殑鍏辨€с€?/p>

Throwable 鏈変袱涓噸瑕佺殑瀛愮被锛欵xception锛堝紓甯革級鍜?Error锛堥敊璇級锛屼簩鑰呴兘鏄?Java 寮傚父澶勭悊鐨勯噸瑕佸瓙绫伙紝鍚勮嚜閮藉寘鍚ぇ閲忓瓙绫汇€?/p>

Exception锛堝紓甯革級鏄簲鐢ㄧ▼搴忎腑鍙兘鐨勫彲棰勬祴銆佸彲鎭㈠闂銆?/p>

涓€鑸ぇ澶氭暟寮傚父琛ㄧず涓害鍒拌交搴︾殑闂銆?/p>

寮傚父涓€鑸槸鍦ㄧ壒瀹氱幆澧冧笅浜х敓鐨勶紝閫氬父鍑虹幇鍦ㄤ唬鐮佺殑鐗瑰畾鏂规硶鍜屾搷浣滀腑銆?/p>

鍦?EchoInput 绫讳腑锛屽綋璇曞浘璋冪敤 readLine 鏂规硶鏃讹紝鍙兘鍑虹幇 IOException 寮傚父銆?/p>

Error锛堥敊璇級琛ㄧず杩愯搴旂敤绋嬪簭涓緝涓ラ噸闂銆?/p>

澶у鏁伴敊璇笌浠g爜缂栧啓鑰呮墽琛岀殑鎿嶄綔鏃犲叧锛岃€岃〃绀轰唬鐮佽繍琛屾椂 JVM(Java 铏氭嫙鏈?鍑虹幇鐨勯棶棰樸€?/p>

渚嬪锛屽綋 JVM 涓嶅啀鏈夌户缁墽琛屾搷浣滄墍闇€鐨勫唴瀛樿祫婧愭椂锛屽皢鍑虹幇 OutOfMemoryError銆?/p>

Exception 绫绘湁涓€涓噸瑕佺殑瀛愮被 RuntimeException銆?/p>

RuntimeException 绫诲強鍏跺瓙绫昏〃绀衡€淛VM 甯哥敤鎿嶄綔鈥濆紩鍙戠殑閿欒銆?/p>

渚嬪锛岃嫢璇曞浘浣跨敤绌哄€煎璞″紩鐢ㄣ€侀櫎鏁颁负闆舵垨鏁扮粍瓒婄晫锛屽垯鍒嗗埆寮曞彂杩愯鏃跺紓甯革紙NullPointerException銆丄rithmeticException锛夊拰 ArrayIndexOutOfBoundException銆?/p>

Java 寮傚父鐨勫鐞?鍦?Java 搴旂敤绋嬪簭涓紝瀵瑰紓甯哥殑澶勭悊鏈変袱绉嶆柟寮忥細澶勭悊寮傚父鍜屽0鏄庡紓甯搞€?/p>

澶勭悊寮傚父锛歵ry銆乧atch 鍜?finally 鑻ヨ鎹曡幏寮傚父锛屽垯蹇呴』鍦ㄤ唬鐮佷腑娣诲姞寮傚父澶勭悊鍣ㄥ潡銆?/p>

杩欑 Java 缁撴瀯鍙兘鍖呭惈 3 涓儴鍒嗭紝 閮芥湁 Java 鍏抽敭瀛椼€?/p>

鍦╦ava涓殑寮傚父澶勭悊涓殑寮傚父瀵硅薄鏄粈涔?/h2>

Exception绫讳互鍙婁粬鐨勫瓙绫?鐨勪竴涓疄渚嬪璞℃瘮濡傚父瑙佸紓甯?. java.lang.nullpointerexception 杩欎釜寮傚父澶у鑲畾閮界粡甯搁亣鍒帮紝寮傚父鐨勮В閲婃槸锛傜▼搴忛亣涓婁簡绌烘寚閽堬紓锛岀畝鍗曞湴璇村氨鏄皟鐢ㄤ簡鏈粡鍒濆鍖栫殑瀵硅薄鎴栬€呮槸涓嶅瓨鍦ㄧ殑瀵硅薄锛岃繖涓敊璇粡甯稿嚭鐜板湪鍒涘缓鍥剧墖锛岃皟鐢ㄦ暟缁勮繖浜涙搷浣滀腑锛屾瘮濡傚浘鐗囨湭缁忓垵濮嬪寲锛屾垨鑰呭浘鐗囧垱寤烘椂鐨勮矾寰勯敊璇瓑绛夈€?/p>

瀵规暟缁勬搷浣滀腑鍑虹幇绌烘寚閽堬紝寰堝鎯呭喌涓嬫槸涓€浜涘垰寮€濮嬪涔犵紪绋嬬殑鏈嬪弸甯哥姱鐨勯敊璇紝鍗虫妸鏁扮粍鐨勫垵濮嬪寲鍜屾暟缁勫厓绱犵殑鍒濆鍖栨贩娣嗚捣鏉ヤ簡銆?/p>

鏁扮粍鐨勫垵濮嬪寲鏄鏁扮粍鍒嗛厤闇€瑕佺殑绌洪棿锛岃€屽垵濮嬪寲鍚庣殑鏁扮粍锛屽叾涓殑鍏冪礌骞舵病鏈夊疄渚嬪寲锛屼緷鐒舵槸绌虹殑锛屾墍浠ヨ繕闇€瑕佸姣忎釜鍏冪礌閮借繘琛屽垵濮嬪寲锛堝鏋滆璋冪敤鐨勮瘽锛?2. java.lang.classnotfoundexception 杩欎釜寮傚父鏄緢澶氬師鏈湪jb绛夊紑鍙戠幆澧冧腑寮€鍙戠殑绋嬪簭鍛橈紝鎶妀b涓嬬殑绋嬪簭鍖呮斁鍦╳tk涓嬬紪璇戠粡甯稿嚭鐜扮殑闂锛屽紓甯哥殑瑙i噴鏄紓鎸囧畾鐨勭被涓嶅瓨鍦紓锛岃繖閲屼富瑕佽€冭檻涓€涓嬬被鐨勫悕绉板拰璺緞鏄惁姝g‘鍗冲彲锛屽鏋滄槸鍦╦b涓嬪仛鐨勭▼搴忓寘锛屼竴鑸兘鏄粯璁ゅ姞涓妏ackage鐨勶紝鎵€浠ヨ浆鍒皐tk涓嬪悗瑕佹敞鎰忔妸package鐨勮矾寰勫姞涓娿€?/p>

3. java.lang.arithmeticexception 杩欎釜寮傚父鐨勮В閲婃槸锛傛暟瀛﹁繍绠楀紓甯革紓锛屾瘮濡傜▼搴忎腑鍑虹幇浜嗛櫎浠ラ浂杩欐牱鐨勮繍绠楀氨浼氬嚭杩欐牱鐨勫紓甯革紝瀵硅繖绉嶅紓甯革紝澶у灏辫濂藉ソ妫€鏌ヤ竴涓嬭嚜宸辩▼搴忎腑娑夊強鍒版暟瀛﹁繍绠楃殑鍦版柟锛屽叕寮忔槸涓嶆槸鏈変笉濡ヤ簡銆?/p>

4. java.lang.arrayindexoutofboundsexception 杩欎釜寮傚父鐩镐俊寰堝鏈嬪弸涔熺粡甯搁亣鍒拌繃锛屽紓甯哥殑瑙i噴鏄紓鏁扮粍涓嬫爣瓒婄晫锛傦紝鐜板湪绋嬪簭涓ぇ澶氶兘鏈夊鏁扮粍鐨勬搷浣滐紝鍥犳鍦ㄨ皟鐢ㄦ暟缁勭殑鏃跺€欎竴瀹氳璁ょ湡妫€鏌ワ紝鐪嬭嚜宸辫皟鐢ㄧ殑涓嬫爣鏄笉鏄秴鍑轰簡鏁扮粍鐨勮寖鍥达紝涓€鑸潵璇达紝鏄剧ず锛堝嵆鐩存帴鐢ㄥ父鏁板綋涓嬫爣锛夎皟鐢ㄤ笉澶鏄撳嚭杩欐牱鐨勯敊锛屼絾闅愬紡锛堝嵆鐢ㄥ彉閲忚〃绀轰笅鏍囷級璋冪敤灏辩粡甯稿嚭閿欎簡锛岃繕鏈変竴绉嶆儏鍐碉紝鏄▼搴忎腑瀹氫箟鐨勬暟缁勭殑闀垮害鏄€氳繃鏌愪簺鐗瑰畾鏂规硶鍐冲畾鐨勶紝涓嶆槸浜嬪厛澹版槑鐨勶紝杩欎釜鏃跺€欙紝鏈€濂藉厛鏌ョ湅涓€涓嬫暟缁勭殑length锛屼互鍏嶅嚭鐜拌繖涓紓甯搞€?/p>

5. java.lang.illegalargumentexception 杩欎釜寮傚父鐨勮В閲婃槸锛傛柟娉曠殑鍙傛暟閿欒锛傦紝寰堝j2me鐨勭被搴撲腑鐨勬柟娉曞湪涓€浜涙儏鍐典笅閮戒細寮曞彂杩欐牱鐨勯敊璇紝姣斿闊抽噺璋冭妭鏂规硶涓殑闊抽噺鍙傛暟濡傛灉鍐欐垚璐熸暟灏变細鍑虹幇杩欎釜寮傚父锛屽啀姣斿g.setcolor(int red,int green,int blue)杩欎釜鏂规硶涓殑涓変釜鍊硷紝濡傛灉鏈夎秴杩?55鐨勪篃浼氬嚭鐜拌繖涓紓甯革紝鍥犳涓€鏃﹀彂鐜拌繖涓紓甯革紝鎴戜滑瑕佸仛鐨勶紝灏辨槸璧剁揣鍘绘鏌ヤ竴涓嬫柟娉曡皟鐢ㄤ腑鐨勫弬鏁颁紶閫掓槸涓嶆槸鍑虹幇浜嗛敊璇€?/p>

6. java.lang.illegalaccessexception 杩欎釜寮傚父鐨勮В閲婃槸锛傛病鏈夎闂潈闄愶紓锛屽綋搴旂敤绋嬪簭瑕佽皟鐢ㄤ竴涓被锛屼絾褰撳墠鐨勬柟娉曞嵆娌℃湁瀵硅绫荤殑璁块棶鏉冮檺渚夸細鍑虹幇杩欎釜寮傚父銆?/p>

瀵圭▼搴忎腑鐢ㄤ簡package鐨勬儏鍐典笅瑕佹敞鎰忚繖涓紓甯搞€?/p>

鍏朵粬杩樻湁寰堝寮傚父锛屾垜灏变笉涓€涓€鍒椾妇浜嗭紝鎴戣璇存槑鐨勬槸锛屼竴涓悎鏍肩殑绋嬪簭鍛橈紝闇€瑕佸绋嬪簭涓父瑙佺殑闂鏈夌浉褰撶殑浜嗚В鍜岀浉搴旂殑瑙e喅鍔炴硶锛屽惁鍒欎粎浠呭仠鐣欏湪鍐欑▼搴忚€屼笉浼氭敼绋嬪簭鐨勮瘽锛屼細鏋佸ぇ褰卞搷鍒拌嚜宸辩殑寮€鍙戠殑銆?/p>

鍏充簬寮傚父鐨勫叏閮ㄨ鏄庯紝鍦╝pi閲岄兘鍙互鏌ラ槄銆?/p>

绠楁湳寮傚父绫伙細ArithmeticExecption 绌烘寚閽堝紓甯哥被锛歂ullPointerException 绫诲瀷寮哄埗杞崲寮傚父锛欳lassCastException 鏁扮粍璐熶笅鏍囧紓甯革細NegativeArrayException 鏁扮粍涓嬫爣瓒婄晫寮傚父锛欰rrayIndexOutOfBoundsException 杩濊儗瀹夊叏鍘熷垯寮傚父锛歋ecturityException 鏂囦欢宸茬粨鏉熷紓甯革細EOFException 鏂囦欢鏈壘鍒板紓甯革細FileNotFoundException 瀛楃涓茶浆鎹负鏁板瓧寮傚父锛歂umberFormatException 鎿嶄綔鏁版嵁搴撳紓甯革細SQLException 杈撳叆杈撳嚭寮傚父锛欼OException 鏂规硶鏈壘鍒板紓甯革細NoSuchMethodException java.lang.AbstractMethodError 鎶借薄鏂规硶閿欒銆?/p>

褰撳簲鐢ㄨ瘯鍥捐皟鐢ㄦ娊璞℃柟娉曟椂鎶涘嚭銆?/p>

java.lang.AssertionError 鏂█閿欍€?/p>

鐢ㄦ潵鎸囩ず涓€涓柇瑷€澶辫触鐨勬儏鍐点€?/p>

java.lang.ClassCircularityError 绫诲惊鐜緷璧栭敊璇€?/p>

鍦ㄥ垵濮嬪寲涓€涓被鏃讹紝鑻ユ娴嬪埌绫讳箣闂村惊鐜緷璧栧垯鎶涘嚭璇ュ紓甯搞€?/p>

java.lang.ClassFormatError 绫绘牸寮忛敊璇€?/p>

褰揓ava铏氭嫙鏈鸿瘯鍥句粠涓€涓枃浠朵腑璇诲彇Java绫伙紝鑰屾娴嬪埌璇ユ枃浠剁殑鍐呭涓嶇鍚堢被鐨勬湁鏁堟牸寮忔椂鎶涘嚭銆?/p>

java.lang.Error 閿欒銆?/p>

鏄墍鏈夐敊璇殑鍩虹被锛岀敤浜庢爣璇嗕弗閲嶇殑绋嬪簭杩愯闂銆?/p>

杩欎簺闂閫氬父鎻忚堪涓€浜涗笉搴旇搴旂敤绋嬪簭鎹曡幏鐨勫弽甯告儏鍐点€?/p>

java.lang.ExceptionInInitializerError 鍒濆鍖栫▼搴忛敊璇€?/p>

褰撴墽琛屼竴涓被鐨勯潤鎬佸垵濮嬪寲绋嬪簭鐨勮繃绋嬩腑锛屽彂鐢熶簡寮傚父鏃舵姏鍑恒€?/p>

闈欐€佸垵濮嬪寲绋嬪簭鏄寚鐩存帴鍖呭惈浜庣被涓殑static璇彞娈点€?/p>

java.lang.IllegalAccessError 杩濇硶璁块棶閿欒銆?/p>

褰撲竴涓簲鐢ㄨ瘯鍥捐闂€佷慨鏀规煇涓被鐨勫煙锛團ield锛夋垨鑰呰皟鐢ㄥ叾鏂规硶锛屼絾鏄張杩濆弽鍩熸垨鏂规硶鐨勫彲瑙佹€у0鏄庯紝鍒欐姏鍑鸿寮傚父銆?/p>

java.lang.IncompatibleClassChangeError 涓嶅吋瀹圭殑绫诲彉鍖栭敊璇€?/p>

褰撴鍦ㄦ墽琛岀殑鏂规硶鎵€渚濊禆鐨勭被瀹氫箟鍙戠敓浜嗕笉鍏煎鐨勬敼鍙樻椂锛屾姏鍑鸿寮傚父銆?/p>

涓€鑸湪淇敼浜嗗簲鐢ㄤ腑鐨勬煇浜涚被鐨勫0鏄庡畾涔夎€屾病鏈夊鏁翠釜搴旂敤閲嶆柊缂栬瘧鑰岀洿鎺ヨ繍琛岀殑鎯呭喌涓嬶紝瀹规槗寮曞彂璇ラ敊璇€?/p>

java.lang.InstantiationError 瀹炰緥鍖栭敊璇€?/p>

褰撲竴涓簲鐢ㄨ瘯鍥鹃€氳繃Java鐨刵ew鎿嶄綔绗︽瀯閫犱竴涓娊璞$被鎴栬€呮帴鍙f椂鎶涘嚭璇ュ紓甯? java.lang.InternalError 鍐呴儴閿欒銆?/p>

鐢ㄤ簬鎸囩ずJava铏氭嫙鏈哄彂鐢熶簡鍐呴儴閿欒銆?/p>

java.lang.LinkageError 閾炬帴閿欒銆?/p>

璇ラ敊璇強鍏舵墍鏈夊瓙绫绘寚绀烘煇涓被渚濊禆浜庡彟澶栦竴浜涚被锛屽湪璇ョ被缂栬瘧涔嬪悗锛岃渚濊禆鐨勭被鏀瑰彉浜嗗叾绫诲畾涔夎€屾病鏈夐噸鏂扮紪璇戞墍鏈夌殑绫伙紝杩涜€屽紩鍙戦敊璇殑鎯呭喌銆?/p>

java.lang...

杞浇璇锋敞鏄庡嚭澶?a href="http://morvybztv.com/">缁忔祹绠$悊缃?/a> » java 寮傚父绠$悊