【正文】
throws中,并用逗號(hào)分割開(kāi)來(lái)。聲明拋出異常類(lèi)名列表,就使得異常對(duì)象可以依次向后查找,直到有合適的方法捕獲他為止。實(shí)例見(jiàn)下面“ ”一節(jié)。 ? 在系統(tǒng)不能識(shí)別的創(chuàng)建用戶(hù)自定義的異常時(shí),需要編程者在程序中合適位置創(chuàng)建自定義異常的對(duì)象,并利用 throw語(yǔ)句將這個(gè)新異常對(duì)象拋出。 上一頁(yè) 返回 ? 本例演示了如何對(duì)異常條件生成用戶(hù)定義的異常類(lèi)層,和如何用這些用戶(hù)定義的異常類(lèi)編寫(xiě)程序來(lái)處理異常條件。例子的第一部分構(gòu)造了一個(gè)報(bào)告年齡相關(guān)異常的異常類(lèi)層,如 圖52所示。 ? 這個(gè)層次的根為 AgeException類(lèi)。它有一個(gè)數(shù)據(jù)成員Age,用于防治導(dǎo)致異常發(fā)生的年齡。它有兩個(gè)子類(lèi):類(lèi)OutOfAgeLimtException用了對(duì)于特定任務(wù)年齡太小或太老的情況,而類(lèi) IllegalAgeFormatException用于年齡在合法范圍以外或格式有錯(cuò)誤的情況。前一個(gè)類(lèi)用數(shù)據(jù)成員 age linmit 來(lái)設(shè)置可接受的年齡極限,定義這些類(lèi)的程序如下: 下一頁(yè) 返回 ? Import java . io .*。 ? Class AgeException extends Exception { ? Int age。 ? AgeException() { ? Super()。 ? } ? AgeException(String message) { ? Super(message)。 ? } ? } 上一頁(yè) 下一頁(yè) 返回 ? Class OutOfAgeLimtException extends AgeException { ? Int ageLimit。 ? OutOfAgeLimtException extends AgeException { ? Super(message)。 ? } ? OutOfAgeLimtException(int ageLimit,String message){ ? Super(message)。 ? =ageLimit。 ? } ? } 上一頁(yè) 下一頁(yè) 返回 ? Class Too Young Exception extends OutOfAgeLimtException { ? Too Young Exception() { ? Super(“ too young” )。 ? } ? Too Young Exception(int age,int ageLimit,String message){ ? Super(ageLimit ,” You are too young to” +message +” .” )。 ? This . age=age。 ? } ? } 上一頁(yè) 下一頁(yè) 返回 ? Class TooOldException extends OutOfAgeLimtException { ? TooOldException() { ? Super(“ too old” )。 ? } ? TooOldException(int age,int ageLimit,String message) { ? Super(ageLimit,” You are too old to” +message +” .” )。 ? =age。 ? } ? } 上一頁(yè) 下一頁(yè) 返回 ? class IllegalAgeFormatException extends AgeException { ? IllegalAgeFormatException (String message) { ? Super(message)。 ? } ? IllegalAgeFormatException() { ? Super(“ Illegal age format” )。 ? } ? } 上一頁(yè) 下一頁(yè) 返回 ? Class NegativeAgeExcption extends IllegalAgeFormatException { ? Negative AgeExcption(String message) { ? Super(message)。 ? } ? Negative AgeExcption(int age) { ? Super(“ Age must be nonnegative.” )。 ? =age。 ? } ? } 上一頁(yè) 下一頁(yè) 返回 ? 例子的第二部分是使用前構(gòu)造的異常類(lèi)層的程序。本程序在不同年齡之間循環(huán),看看特定年齡的人能否作過(guò)山車(chē)。如果年齡太小、太大或是負(fù)數(shù),則方法RideRollerCoasterAtAge()會(huì)分別拋出 Too Young Exception、 TooOldException和NegativeageExcption。程序列出如下: ? Public class AgeException Test{ ? Static PrintWriter out=new PrintWriter(System . out, true)。 ? Static void rideRollerCoaxterAtAge(int age) ? Throws NetativeAgeExcption,OutOfAgeLimtException { ? Out. printIn(“ Trying to ride a roller coaster at age “ +age+” …” )。 上一頁(yè) 下一頁(yè) 返回 ? If (age0) ? Throw new Negative AgeExcption(age)。 ? Else if(age5) ? Throw new Too Young Exception(age , 5 ,” ride a roller coaster “ )。 ? Else if(age45) ? Throw nes TooOldException(age ,45 ,” ride a roller coaster” )。 ? Out_printIn(“ Riding the roller coaster…” )。 ? } 上一頁(yè) 下一頁(yè) 返回 ? Public static void main (String args{}) { ? Int ages{}={3 ,2 ,10 ,35 ,65}。 ? For(int i=0。iages,length。i++) ? Try { ? rideRollerCoasterAtAge(ages[i])。 ? out . printIn(“ Wow!What an experience!” )。 ? }catch(OutOfAgeLimtException e) { ? out . printIn(() )。 上一頁(yè) 下一頁(yè) 返回 ? if (ages[i]e .ageLimit) ? out . printIn( (ages[i])+” more years and you can try it .” )。 ? else ? out . printIn( (ages[i]) ? +” years ago riding it was like a piece of cake .” )。 ? }catch(Negative AgeExcption e) { ? (() )。 ? }finally{ ? Out. printIn()。 ? } ? } ? } 上一頁(yè) 下一頁(yè) 返回 ? 上述程序的輸出如下: ? Trying to ride a roller coaster at age 3… ? Age must be nonnegative. ? Trying to ride a roller coaster at ago 2… ? You are too young to ride a roller coaster. ? 3 more years and you’ ll be able to try it. ? Trying to ride a roller coaster at age 10… ? Riding the roller coaster… 上一頁(yè) 下一頁(yè) 返回 ? Wow! What an wxperience! ? Trying to ride a roller coaste at age 35… ? Riding the roller coaster… ? Wow! What an wxperience! ? Trying to ride a roller coaster at age 65… ? You are too old to ride a roller coaster. ? 20 years ago riding it was like a piece of cake. 上一頁(yè) 返回 圖 52 AgeException類(lèi)的大層次 返回 謝謝觀看 /歡迎下載 BY FAITH I MEAN A VISION OF GOOD ONE CHERISHES AND THE ENTHUSIASM THAT PUSHES ONE TO SEEK ITS FULFILLMENT REGARDLESS OF OBSTACLES. BY FAITH I BY FAITH