基礎異常第1天什么Java代碼在編譯、運行時期發(fā)生的問題就是_第1頁
基礎異常第1天什么Java代碼在編譯、運行時期發(fā)生的問題就是_第2頁
基礎異常第1天什么Java代碼在編譯、運行時期發(fā)生的問題就是_第3頁
基礎異常第1天什么Java代碼在編譯、運行時期發(fā)生的問題就是_第4頁
基礎異常第1天什么Java代碼在編譯、運行時期發(fā)生的問題就是_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

21第1章異常什么是異常?JavaJava中,把異常信息封裝成了一個類。當出現(xiàn)了問題時,就會創(chuàng)建異常類對象并拋出異常相JavaExceptionAPIException的描述,ExceptionThrowableExceptionThrowable。ThrowableJava語言中所有錯誤或異常的超類,即類。另外,在異常Exception類中,有一個子類要特殊說明一下,RuntimeException子類,RuntimeExceptionJavaThrowable:Throwable:它是所有錯誤與異常的超類 類|Error|Exception編譯期異常,JAVA|RuntimeException運行期異常JAVApublicpublicstaticvoidmain(String[]args){int[]arr=newint[3];//該句運行時發(fā)生了數(shù)組索引越界異常 System.out.println("over}publicpublicstaticvoidmain(String[]args){int[]arr=newint[1024*1024*100];}先運行下面的程序,程序會產(chǎn)生一個數(shù)組索引越界異常ArrayIndexOfBound classclasspublicstaticintge ement(int[]arr,intindex){intelement=arr[index];return}}classclassExceptionDemo2publicstaticvoidmain(String[]args){int[]arr={34,12,67};intnum=ArrayTools.ge }}拋出異常在編寫程序時,須要考慮程序出現(xiàn)問題的情況。比如,在定義方法時,方法需要接受參2throwthrowthrowthrownew異常類名(參數(shù)下面是異常類下面是異常類 ception與NullPointerException的構造方thrownewNullPointerException(" thrownew throwclassclasspublicstaticint ement(int[]arr,intindex)thrownewNullPointerException("arr指向的數(shù)組不存在}if(index<0||thrownew 組中不存在}intelement=arr[index];returnelement;}}classclassExceptionDemo3publicstaticvoidmain(String[argsint[]arr={34,12,67};//創(chuàng)建數(shù)組intnum ///intnum /}}異常throw拋出了編譯時異常,而沒有捕獲處理(稍后講解該方式,那么必須通過throws進行,讓調用者去處理。修飾符返回值類型方法名修飾符返回值類型方法名(參數(shù))throws異常類名1,異常類名2… classclass publicvoidshow(intx)throwsException thrownew}elseSystem.out.println("show}}}throws用于進行異常類的 多個異常的情況,例如多個異常的情況,例如publicstaticintge ement(int[]arr,intindex)throwsNullPointerException, ception{thrownewNullPointerException("arr指向的數(shù)組不存在}if(index<0||thrownew 存在}intelement=arr[index];returnelement;}}捕獲異常try}try}catch(}finally}finallyfinally代碼塊中存放的代碼都是一定會classclasstryint[]arr=newSystem.out.println(arr[5]);//會拋出 }catch(ArrayIndexOutOfBound ceptione){//括號中需要定義什么呢?try中拋出}finallyarrnullnull}}}try…catch…finallytrycatchfinallycatchfinallyvoidshow()throwsthrowvoidshow()throwsthrownewException();//}catch(Exception}}trycatchcatchvoidvoidshow()throwsthrownewException();//}catch(XxxException}catch(YyyException}catch(ZzzException}}catchcatch處理。voidshow(){//throwsthrownewvoidshow(){//throwsthrownew}finally}}classFupublicvoidclassFupublicvoidmethod()throwsRuntimeException}}}classZiextendsFupublicvoidmethod(throwsRuntimeException{}////publicvoidmethod(throwsNullPointerException}當父類方法多個異常時,子類覆蓋時只能多個異常的子集classFupublicvoidclassFupublicvoidmethod}}classZiextendsFupublicvoidmethod(throwsException}classFupublicvoidmethod()throwsNullPointerException,}}classZiextendsFupublicvoidmethod()throwsNullPointerException,ClassCastException{}publicvoidmethod(throwsNullPointerException拋出父類異常中的一部分publicvoidmethod()throwsClassCastException{}//拋出父類異常中的一部分}catchcatch的捕獲。萬一問題處理不了呢?catchRuntimeExceptioninterfaceInter void}classZiimplementsInterpublicvoidmethod(){//無法 throwsExceptionint[]arr=null;if(arr==null)thrownewException(“arr是空的}catch(Exceptionthrowthrownew}}}}ThrowablegetMessagetrytryp=null;if(p==null){thrownewNullPointerException(“}}catch(NullPointerExceptione){Stringmessage=e.getMesage();System.out.println(message);Stringresult=e.toString();}JDK內(nèi)部定義好的,并且這些異常不好找。書寫時也很不方Javathrow拋出。throwpublicpublicclassNullPointerExceptionextendsRuntimeExceptionpublicNullPointerException()}publicNullPointerException(Strings)}}ClassClassextendsException//public異常名}public異常名(Strings){}}ExceptionclassclassMyExceptionextendspublic}publicMyException(Stringmessage)super(message);//}}classclassMyExceptionextends}MyException(Stringmessage)super(message);/}}定義類,包含name與age兩個成員變量NoAgeException異常,異常提示信息“數(shù)值要求:在測試類中,調用有參數(shù)構造方法,完成對象創(chuàng)建,并進行異常的處理classclassNoAgeExceptionextendsException{NoAgeException(){}NoAgeException(Stringmessage){}}類{privateStringname;privateintage;(Stringname,intage)throwsNoAgeExceptionif(age<0|| thrownew 數(shù) }=name;this.age=age;} publicString return }}}classclasspublicstaticvoidmain(String[]args){try{p=new }catch(NoAgeExceptionex){ }}}總結一下,構造函數(shù)到底拋出這個NoAgeException是繼承Exception呢?還是繼承RuntimeExceptionThrowable:Throwable:它是所有錯誤與異常的超類 類|Errorjava|Exception編譯期異常javac.e

溫馨提示

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

評論

0/150

提交評論