lecture11-異常處理PPT幻燈片.ppt_第1頁
lecture11-異常處理PPT幻燈片.ppt_第2頁
lecture11-異常處理PPT幻燈片.ppt_第3頁
lecture11-異常處理PPT幻燈片.ppt_第4頁
lecture11-異常處理PPT幻燈片.ppt_第5頁
已閱讀5頁,還剩50頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、異常處理和異常類型Chapter 11 557-572,Hy Mao 2020年8月15日 華東師范大學軟件學院,1,2020/8/15,2,Outline,異常處理的重要性 Java語言中的異常處理 Java的異常類 如何定義自己的異常: 定義新的異常 拋出異常 捕獲異常 異常的處理流程 調(diào)試技術(shù)與調(diào)試技巧,華東師范大學軟件學院 王麗蘋,2020/8/15,3,異常介紹,Example:以下代碼運行之后會出現(xiàn)什么情況?,public class ExceptionTest public static void main(String args) int b=0; int a; a = 4/b

2、; ,C:java ExceptionTest Exception in thread main java.lang.ArithmeticException: / by zero at ExceptionTest.main(ExceptionTest.java:4),華東師范大學軟件學院 王麗蘋,2020/8/15,4,異常介紹,public class ArrayExceptionTest public static void main(String args) int arr = new int5; System.out.println(arr1); System.out.println(

3、arr5); System.out.println(arr2); ,C:java ArrayExceptionTest 0 Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 5 at ArrayExceptionTest.main(ArrayExceptionTest.java:5),華東師范大學軟件學院 王麗蘋,2020/8/15,5,異常簡介,什么是異常? 異常實際上是導(dǎo)致正常指令流中斷的一種事件. 編寫應(yīng)用程序時應(yīng)該做些什么工作(程序中的Bug是不可避免的,編寫程序時應(yīng)該盡量的避免錯誤的出現(xiàn),并且提供盡可

4、能多的錯誤信息): 通知用戶出現(xiàn)了一個錯誤。 保存當前已經(jīng)完成的所有工作。 允許用戶安全地退出程序。,華東師范大學軟件學院 王麗蘋,2020/8/15,6,異常簡介,什么類型的錯誤可以導(dǎo)致異常呢? 嚴重的硬件錯誤,如內(nèi)存中某些芯片故障或者硬盤崩潰。 程序需要使用系統(tǒng)中當前不可用的I/O設(shè)備 試圖用零去除 試圖通過超過邊界的下標訪問數(shù)組的元素 整數(shù)溢出 浮點數(shù)溢出 試圖打開不存在的文件以使用 試圖打破安全性,比如試圖修改只讀文件,華東師范大學軟件學院 王麗蘋,2020/8/15,7,傳統(tǒng)的錯誤處理技術(shù),在一般的傳統(tǒng)的語言中如何處理異常事件? 例如:如何處理除數(shù)為0的事件。 早期除了精心設(shè)計的之外

5、,編程語言沒有任何的錯誤處理機制。 程序員必須考慮可能的錯誤條件并沿著主應(yīng)用程序代碼進行檢查。 程序員認真地考慮所有類型的錯誤檢測,是非常巨大的任務(wù)。,華東師范大學軟件學院 王麗蘋,2020/8/15,8,傳統(tǒng)的錯誤處理技術(shù),以常規(guī)方法處理錯誤退出程序,返回錯誤編號,public void insert(QueueElement d) if (isFull() System.out.println(queue overflow) ; System.exit(1) ; else if (d.getname().isexist() System.out.println(queueElementna

6、me is exist ) ; System.exit(2) ; else if(d.getname().isnull() System.out.println(queueelement name is null) ; System.exit(3) ; else / insert the element / end of method insert,華東師范大學軟件學院 王麗蘋,2020/8/15,9,Note: 常規(guī)方法的不足:,大部分精力花在出錯處理上了. 只把能夠想到的錯誤考慮到,對以外的情況無法處理 程序可讀性差 出錯返回信息量太少,Java中用異常的形式處理錯誤,華東師范大學軟件學院

7、 王麗蘋,2020/8/15,10,Java中處理異常的思路,Java中的異常可以理解為一種特殊的class。 Java中用異常來處理程序中可能的錯誤,其主要的特點在于: 1.把錯誤代碼從常規(guī)代碼中分離出來(便于集中精力處理特定的功能)。 2. 把錯誤傳播給調(diào)用堆棧。 3. 按錯誤類型和錯誤差別分組(復(fù)用的思想)。 4. 系統(tǒng)提供了對于一些無法預(yù)測的錯誤的捕獲和處理 5. 克服了傳統(tǒng)方法的錯誤信息有限的問題,華東師范大學軟件學院 王麗蘋,2020/8/15,11,Exception如何捕獲,1. try 2. / 接受監(jiān)視的程序塊,在此區(qū)域可能產(chǎn)生異常 3. catch (SomeExcept

8、ionType e) 4. / 對SomeException出現(xiàn)時的處理 5. catch (Exception e) 6. / 對Exception出現(xiàn)時的處理 7. ,In try, Exception thrown here,First catch, exception handler,Second catch, exception handler,Thrown exception matched against first set of exception handlers,If it fails to match. It is matched against the next set

9、 of exception handlers and so on,If the exception matches none of the exception handlers, the program is abandoned,捕獲并且處理異常的格式,華東師范大學軟件學院 王麗蘋,2020/8/15,12,Exception如何捕獲,try程序塊中通常包含了java程序中的一段代碼,這段代碼在運行中可能會出現(xiàn)異常的事件。try意味著需要檢測這段代碼的執(zhí)行。 catch程序塊: 語法為:catch (ExceptionClass exceptionObj) catch程序塊必須出現(xiàn)在try程序

10、塊之后。一個try程序塊后可以跟一個或多個catch程序塊。 當try程序塊中沒有出現(xiàn)異常時,catch程序塊直接被跳過。 當try程序塊中出現(xiàn)異常時,與當前異常匹配的catch程序塊被執(zhí)行。 具體情況可以參考以下的實例。,華東師范大學軟件學院 王麗蘋,2020/8/15,13,Example-ExceptionTest.java,public class ExceptionTest public static void main(String args) int b=0; int a; try a=4/b; catch(ArithmeticException e) System.out.pr

11、intln(divided by 0); ,本例題為完成除法運算的兩種方式:有異常處理和沒有異常處理,public class ExceptionTest public static void main(String args) int b=0; int a; a = 4/b; ,Exception in thread main java.lang.ArithmeticException: / by zero at ExceptionTest.main(ExceptionTest.java:4),divided by 0,華東師范大學軟件學院 王麗蘋,2020/8/15,14,Example-

12、ExceptionTest.java,public class ExceptionTest public static void main(String args) int b=0; int a; try a=4/b; catch(NullPointerException e) System.out.println(divided by 0); ,雖然有異常處理但是類型不匹配所以輸出結(jié)果與沒有異常處理時的一樣,Exception in thread main java.lang.ArithmeticException: / by zero at ExceptionTest.main(Excep

13、tionTest.java:25),華東師范大學軟件學院 王麗蘋,2020/8/15,15,Java中定義的異常,異常的分類,華東師范大學軟件學院 王麗蘋,2020/8/15,16,Java中定義的異常,Error:描述了java運行時間系統(tǒng)內(nèi)部的錯誤以及資源耗盡的情況。用戶一般對這種錯誤是無能為力的。(例如虛擬機內(nèi)部的錯誤、內(nèi)存溢出、堆棧溢出的錯誤) 異常的分類: Unchecked Exception是指Error類和RuntimeException類及其他們的子類。 RuntimeException及其子類表示的異常往往是由于程序編寫的錯誤造成的。 例如數(shù)組越界錯誤,空指針錯誤等,這一類

14、錯誤往往可以通過改寫程序來避免。 在程序中可以不捕獲Unchecked Exception。,華東師范大學軟件學院 王麗蘋,2020/8/15,17,Java中定義的異常,異常的的分類: Checked Exception是指除了Error類和RuntimeException類及其他們的子類之外的異常類。 Checked Exception往往表示由于外界的因素造成的系統(tǒng)異常事件例如I/O錯誤,讀取硬盤錯誤等,這一類異常在程序代碼中是沒有辦法解決的。 如果在代碼中存在Checked Exception,必須要用try和catch程序塊開處理這一類異常事件。,華東師范大學軟件學院 王麗蘋,202

15、0/8/15,18,Java中定義的異常,Java程序中常用的異常類 RuntimeException: 錯誤的轉(zhuǎn)型(cast):java.lang.ClassCastException 數(shù)組越界錯誤:java.lang.ArrayIndexOutOfBoundsException 空指針錯誤:java.lang.NullPointerException 數(shù)組元素類型錯誤:java.lang.ArrayStoreException 算術(shù)運算錯誤:java.lang.ArithmeticException String轉(zhuǎn)換成數(shù)字類型時的錯誤:java.lang.NumberFormatExcep

16、tion 非RuntimeException: 文件讀取越界:java.io.EOFException 文件沒有找到:java.io.FileNotFoundException URL鏈接錯誤:.MalformedURLException 主機不可識別:.UnknownHostException,華東師范大學軟件學院 王麗蘋,2020/8/15,19,ExampleStrDiv.java,現(xiàn)在存在兩個String,他們的值為數(shù)值型的。請編寫程序完成如下功能:第一個String的第3位和第4位數(shù)字與第二個String的第2位數(shù)字相除的結(jié)果。,public class StrDiv public

17、int Div(String str1, String str2) char c1 = str1.charAt(2); char c2 =str1.charAt(3); char c3 =str2.charAt(1); int num = (int)(c1-48)*10+(int)(c2-48); int num1 = (int)(c3-48); return num/num1; public static void main(String args) StrDiv test = new StrDiv(); System.out.println(result is +test.Div(1234

18、,12);,D:javaForLecturesrclecture9java StrDiv result is 17,Note:這種處理方式能夠成功,但是對于很多可能出問題的情況沒有考慮。所以這種處理方式是不完善。,華東師范大學軟件學院 王麗蘋,2020/8/15,20,ExampleStrDiv.java,public class StrDiv public int Div(String str1, String str2) char c1,c2,c3; int num = 0,num1 =0; try c1 = str1.charAt(2); c2 = str1.charAt(3); c3

19、= str2.charAt(1); num = (int)(c1-48)*10+(int)(c2-48); num1 = (int)(c3-48); return num/num1; catch (StringIndexOutOfBoundsException se) System.out.println(se.getMessage(); catch (ArithmeticException ae) System.out.println(ae.getMessage(); return -1; public static void main(String args) StrDiv test =

20、new StrDiv(); System.out.println(result is +test.Div(1234,12); System.out.println(result is +test.Div(12,12); System.out.println(result is +test.Div(1234,10);,D:javaForLecturesrclecture9java StrDiv result is 17 String index out of range: 2 result is -1 / by zero result is -1,Note:在本程序中捕獲的兩種異常StringI

21、ndexOutOfBoundsException和ArithmeticException都是屬于uncheckedException,Thinking:是否可以總結(jié)一下進行異常處理和沒有進行異常處理的區(qū)別?,華東師范大學軟件學院 王麗蘋,2020/8/15,21,Example-convert.java,完成String向整型的轉(zhuǎn)化。(可能出現(xiàn)的異常,非數(shù)字型的字符串的轉(zhuǎn)換,字符串轉(zhuǎn)換越界。),public class convert public static void main(String args) int num; String str = null; try num = Integ

22、er.parseInt(str);/出現(xiàn)了NumberFormatException System.out.println(-After Convert); catch(NumberFormatException e) System.out.println(e.getMessage()+ cant convert to int); ,D:javaForLecturesrclecture9java convert null cant convert to int,Note:在此題中如果定義的局部變量String str在的程序中沒有初始化就直接使用,編譯會出錯。,華東師范大學軟件學院 王麗蘋,2

23、020/8/15,22,Example-convert.java,public class convert public static void main(String args) int num; String str = null; try str = abc; /非數(shù)字字符轉(zhuǎn)換錯誤 System.out.println(-the String:+str); num = Integer.parseInt(str); System.out.println(-After Convert); catch(NumberFormatException e) System.out.println(e.

24、getMessage()+ cant convert to int); ,D:javaForLecturesrclecture9java convert -the String:abc For input string: abc cant convert to int,Note:請注意在try中異常發(fā)生時,程序中斷的位置。,華東師范大學軟件學院 王麗蘋,2020/8/15,23,Example-convert.java,public class convert public static void main(String args) int num; String str = null; tr

25、y str = “3712312312”; /int是占32個二進制位,str超出了int的取值范圍 System.out.println(-the String:+str); num = Integer.parseInt(str); System.out.println(-After Convert); catch(NumberFormatException e) System.out.println(e.getMessage()+ cant convert to int);,D:javaForLecturesrclecture9java convert -the String: 37123

26、12312 For input string: 3712312312 cant convert to int,華東師范大學軟件學院 王麗蘋,2020/8/15,24,Example-convert.java,public class convert public static void main(String args) int num; String str = null; try str = 3712312312; /int是占32個二進制位,str超出了int的取值范圍 System.out.println(-the String:+str); num = Integer.parseIn

27、t(str); System.out.println(-After Convert); catch(Exception e) System.out.println(e.getMessage()+ Error); ,D:javaForLecturesrclecture9java convert -the String: 3712312312 For input string: 3712312312 Error,Note:在編寫程序的時候,盡可能首先對所有可能的異常信息進行處理,為了避免漏掉某些異常,可以用Exception類來捕獲,但是這個時候提供給用戶的信息量就會少一些。,華東師范大學軟件學院

28、 王麗蘋,2020/8/15,25,Example-convert.java,public class convert public static void main(String args) int num; String str = null; try str = 3712312312; /int是占32個二進制位,str超出了int的取值范圍 System.out.println(-the String:+str); num = Integer.parseInt(str); System.out.println(-After Convert); catch(NumberFormatExc

29、eption e) System.out.println(e.getMessage()+ cant convert to int); System.out.println(After Try and Catch); ,D:javaForLecturesrclecture9java convert -the String:3712312312 For input string: 3712312312 cant convert to int After Try and Catch,華東師范大學軟件學院 王麗蘋,2020/8/15,26,Example-convert.java,public cla

30、ss convert public static void main(String args) int num; String str = null; try str = 3712312312; /int是占32個二進制位,非數(shù)值型字符串轉(zhuǎn)換錯誤 System.out.println(-the String:+str); num = Integer.parseInt(str); System.out.println(-After Convert); catch(NullPointerException e) System.out.println(e.getMessage()+ cant con

31、vert to int); System.out.println(After Try and Catch); ,D:javaForLecturesrclecture9java convert -the String:3712312312 Exception in thread main java.lang.NumberFormatException: For input string: 3 712312312 at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseI

32、nt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at convert.main(convert.java:82),Note:出現(xiàn)異常,但是匹配不成功,程序?qū)⒃诋惓3霈F(xiàn)的位置提前終止,華東師范大學軟件學院 王麗蘋,2020/8/15,27,異常處理流程,1.出現(xiàn)異常找到了所匹配的Exception,Preceding step,Try block Throw statement,Unmatched catch,Matching catch,Unmatched catch,Next step,華東師范大學軟件學院 王

33、麗蘋,2020/8/15,28,異常處理流程,2.出現(xiàn)異常沒有匹配的Exception,Preceding step,Try block Throw statement,Unmatched catch,Unmatched catch,Unmatched catch,Next step,No matching handler,so look for another set of handler,華東師范大學軟件學院 王麗蘋,2020/8/15,29,異常處理流程,3.沒有出現(xiàn)異常,Preceding step,Try block,catch,catch,catch,Next step,華東師范大

34、學軟件學院 王麗蘋,2020/8/15,30,Exercise,public class ArrayExceptionTest public static void main(String args) int arr = new int5; System.out.println(arr1); System.out.println(arr5); System.out.println(arr2); ,以下程序在運行時會出現(xiàn)異常ArrayIndexOutOfBoundsException,請用添加try和catch程序塊處理錯誤,并給用戶用好的提示信息。,華東師范大學軟件學院 王麗蘋,2020/8/

35、15,31,Exception類的構(gòu)造函數(shù),Exception類中的構(gòu)造函數(shù): public Exception() Constructs a new exception with null as its detail message. public Exception(String message) Constructs a new exception with the specified detail message.,Exception類可以創(chuàng)建對象嗎?它的對象創(chuàng)建之后一般是做什么用的呢?,throw語句一般和異常對象關(guān)聯(lián),我們可以在程序中需要的位置自己拋出異常,華東師范大學軟件學院 王

36、麗蘋,2020/8/15,32,throw語句,語法:throw 可以是Throwable及其子類的對象。 例如: NullPointerException ex = new NullPointerException() throw ex; 含義:程序在此位置出現(xiàn)了類型的異常。 如果程序執(zhí)行中遇到了throw語句則正常的流程中斷,轉(zhuǎn)入到catch程序塊中處理異常。,華東師范大學軟件學院 王麗蘋,2020/8/15,33,如何得到關(guān)于異常的描述信息,獲取異常詳細信息的方法在Exception的父類Throwable(java.lang包) 中有定義,Exception及其子類直接繼承了這些方法。

37、常用的方法有: String getMessag()-Returns the detail message string of this Exception. String tostring()-Returns a short description of this Exception. Void printStackTrace()- Prints this exception and its backtrace to the standard error stream.,Note:在編寫程序時,可以在catch()中調(diào)用上述方法以獲取Exception的描述,從而為用戶提供更加友好的信息說明

38、。,華東師范大學軟件學院 王麗蘋,2020/8/15,34,ExampleExceptionMethods.java,public class ExceptionMethods public static void main(String args) try throw new Exception(Heres my Exception); catch(Exception e) System.out.println(Caught Exception); System.out.println( e.getMessage(): + e.getMessage(); System.out.println

39、(e.toString(): + e.toString(); System.out.println(e.printStackTrace():); e.printStackTrace(); ,D:javaForLecturesrclecture9java ExceptionMethods Caught Exception e.getMessage(): Heres my Exception e.toString(): java.lang.Exception: Heres my Exception e.printStackTrace(): java.lang.Exception: Heres my

40、 Exception at ExceptionMethods.main(ExceptionMethods.java:4),華東師范大學軟件學院 王麗蘋,2020/8/15,35,finally,finally一定會執(zhí)行的程序塊(注意不是final) 異常有一個統(tǒng)一的出口-finally程序塊。,try /常規(guī)的代碼; catch() /處理異常 finally /無論發(fā)生什么異常(或者不發(fā)生任何異常),都要執(zhí)行的部分; ,Note:finally代碼段在某些情況下是非常有用的,例如在異常發(fā)生之后我們還需要務(wù)必關(guān)閉數(shù)據(jù)庫的連接,關(guān)閉文件,關(guān)閉已經(jīng)打開的端口,關(guān)閉網(wǎng)絡(luò)連接等。這些都是在一個完整、嚴

41、密的程序中需要考慮的。,華東師范大學軟件學院 王麗蘋,2020/8/15,36,example finally catch(NullPointerException e) System.out.println(in catch get exception: +e.getMessage(); finally System.out.println(In finally, it must executed!); ,D:javaForLecturesrclecture9java JavaFinally in catch get exception: null In finally, it must e

42、xecuted!,Note:出現(xiàn)異常,在catch語句中匹配成功,會執(zhí)行finally 程序塊,華東師范大學軟件學院 王麗蘋,2020/8/15,37,example finally catch(NullPointerException e) System.out.println(in catch get exception+e.getMessage(); finally System.out.println(In finally, it must executed!);,D:javaForLecturesrclecture9java JavaFinally In finally, it mu

43、st executed!,Note:沒有出現(xiàn)異常,會執(zhí)行finally 程序塊,華東師范大學軟件學院 王麗蘋,2020/8/15,38,example finally catch(ArithmeticException e) System.out.println(in catch get exception: +e.getMessage(); finally System.out.println(In finally, it must executed!);,D:javaForLecturesrclecture9java JavaFinally In finally, it must exec

44、uted! Exception in thread main java.lang.NullPointerException at JavaFinally.main(JavaFinally.java:28),Note:出現(xiàn)異常,在catch中沒有匹配成功,會執(zhí)行finally 程序塊,華東師范大學軟件學院 王麗蘋,2020/8/15,39,example finally,public class JavaFinally public static void main(String args) try return; catch(NullPointerException e) System.out

45、.println(in catch get exception: +e.getMessage(); finally System.out.println(In finally, it must executed!);,D:javaForLecturesrclecture9java JavaFinally In finally, it must executed!,Note:在main方法中return退出了方法,但仍然會執(zhí)行finally 程序塊,華東師范大學軟件學院 王麗蘋,2020/8/15,40,關(guān)于throw的進一步說明,throw語句表示出現(xiàn)了該種類型的異常。 一般來說在一個方法的內(nèi)

46、部出現(xiàn)異常時,可以有兩種處理方法: 第一種方法:在方法的內(nèi)部異常出現(xiàn)的地方,即throw語句出現(xiàn)的地方,用try語句捕獲異常,catch語句處理異常。 第二種方法:產(chǎn)生異常的方法不直接處理異常而是由調(diào)用該方法的其他方法來用try和catch程序塊處理異常。,華東師范大學軟件學院 王麗蘋,2020/8/15,41,Throw-Example,public class Test public void division() int num1 = 10; int num2 = 0; if(num2=0) throw new ArithmeticException(); System.out.prin

47、tln(num1 + / + num2 + =+ (num1 / num2); System.out.println(Return from division.); public static void main(String args) Test t = new Test(); try t.division(); catch(ArithmeticException e) System.out.println(Returning from main); ,Pause: Where the exception is caught?,華東師范大學軟件學院 王麗蘋,2020/8/15,42,publ

48、ic class Test public void division() int num1 = 10; int num2 = 0; try if(num2=0) throw new ArithmeticException(); System.out.println(num1 + / + num2 + “=“+ (num1 / num2); catch(ArithmeticException e) e.printStackTrace(); System.out.println(Return from division.); public static void main(String args)

49、 Test t = new Test(); t.division(); System.out.println(Returning from main); ,Pause: Where is the exception caught.,華東師范大學軟件學院 王麗蘋,2020/8/15,43,如何使用自定義的異常,1. 定義自己的異常類 2. 拋出異常(throws throw) 3. 捕獲異常(try,catch,finally),華東師范大學軟件學院 王麗蘋,2020/8/15,44,定義自己的異常類,需要確定你所定義的異常 自定義的異常一般是Java系統(tǒng)中標準的Java Exception類(

50、或者是Throwable,最好是Exception)的子類或者是你已經(jīng)定義好的異常類的子類。,格式: Public class YourNewException extends Exception /end of your Exception class,class NotAValidAttribute extends Exception public NoValidAttribute () public NoValidAttribute (String err) super(err); ,華東師范大學軟件學院 王麗蘋,2020/8/15,45,拋出異常,一般需要兩步來Throw an Exc

51、eption: 第一步:在method header后首先定義方法中將要拋出的異常類型列表,即為throws list 第二步:在需要報告異常的程序部分創(chuàng)建相應(yīng)的異常的實例并且throw 出該實例。即為:throw instance,public void setName(String n) throws NotAValidAttribute if (n = null) throw new NotAValidAttribute(error: employees name is null); else name = n; ,注意:如果在throw語句中用try和catch直接處理了異常,那么th

52、rows語句可以省略。,華東師范大學軟件學院 王麗蘋,2020/8/15,46,Note:關(guān)于throws List的書寫原則,throws語句位于方法頭部的后面,用來聲明方法內(nèi)部已經(jīng)拋出的并且還沒有用try和catch程序塊處理的異常的類型。 關(guān)于throws List的書寫原則 并不是方法中拋出的所有的異常都需要在List中寫出來,對于uncheckedException,可以不在List中寫出來。,華東師范大學軟件學院 王麗蘋,2020/8/15,47,捕獲異常,捕獲自定義的異常和捕獲java中內(nèi)置的異常從實質(zhì)上來說是一樣的。 捕獲自定義異常的條件: 事先定義好了自己的異常類, 已經(jīng)在產(chǎn)

53、生異常的位置拋出了異常 捕獲異常的格式,1. try 2. / 接受監(jiān)視的程序塊,在此區(qū)域可能產(chǎn)生異常 3. catch (MyExceptionType1 e) 4. / 對MyException1出現(xiàn)時的處理 5. catch (MyExceptionType2 e) 6. / 對MyException2出現(xiàn)時的處理 7. ,華東師范大學軟件學院 王麗蘋,2020/8/15,48,Example-定義并使用自己的異常類,Employee類包含的屬性有:姓名,年齡,工資,部門(現(xiàn)在需要控制各個屬性值的有效性。),class NotAValidAttribute extends Excepti

54、on public NotAValidAttribute () public NotAValidAttribute (String err) super(err);,程序?qū)崿F(xiàn)時需要考慮的問題: 屬性,方法都有哪些。 屬性和方法的完整性。姓名的字符長度,年齡的有效范圍,工資的范圍是否有限制。,Note:為了能夠向用戶提供友好的錯誤提示信息,在實現(xiàn)中專門定義了一個類NotAValidAttribute,用來向用戶提供關(guān)于屬性出錯誤時的提示。,華東師范大學軟件學院 王麗蘋,2020/8/15,49,Example-employee/employee.java,public class employee private String name; private int age; private int pay; public employee(String n, int a, int p) throws NotAValidAttribute this.setName(n); this.setAge(a); this.setPay(p); public employee() public void setName(String n)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論