JAVA學習第二十課(異常的應用和注意事項(三)),java學習注意事項

來源:互聯網
上載者:User

JAVA學習第二十課(異常的應用和注意事項(三)),java學習注意事項


十、異常的應用

import java.util.Random;import javax.rmi.CORBA.Stub;/* *老師用電腦上課 *用物件導向思考問題 *問題設計兩個對象 *老師,電腦 * *可能發生的問題: *電腦藍屏、電腦冒煙 */class Lan extends Exception//藍屏{Lan(String problem){super(problem);}}class Fir extends Exception//冒煙{Fir(String problem){super(problem);}}class No extends Exception{No(String problem){super(problem);}}class Computer{//定義一個電腦啟動並執行狀態private int stu = 3; //stu為1正常,2是藍屏,3是冒煙//stu = Random()%2+1;public void run()throws Lan,Fir {switch (stu) {case 2:throw new Lan("Computer is Lan!!!");//此處就不用break了,因為已經拋出了case 3:throw new Fir("Computer is Fir!!!");case 1:System.out.println("Computer is running");break;}}public void rest()//電腦重啟{stu = 0;System.out.println("Computer is rest");}}class Teacher{private String name = null;private Computer C;Teacher(String name) {this.name = name;C = new Computer();}public void lesson()throws Fir,No{try {C.run();System.out.println(name+" teacher is teaching");} catch(Lan a){System.out.println(a.toString());//列印資訊C.rest();lesson();}catch(Fir b){System.out.println(b.toString());//列印資訊test();//能處理的已經處理了,處理不了的繼續向外拋//找人,對電腦進行維修,並告知學校並帶原因,不告知,就是異常異常throw new No("課時無法進行,"+b.getMessage());}}public void test()//做練習{System.out.println("student is exciesing");}}public class Main{public static void main(String[] args){Teacher B = new Teacher("BLF2");try {B.lesson();} catch (Fir e){System.out.println(".....");}catch (No e){System.out.println(e.toString());}finally {//無論課上發生什麼,但是還是會下課System.out.println("到點下課");}}}

/*
 * 異常的轉換,也就叫異常的封裝
 * 資料庫
 * class Nosuess extends Exception
 * {
 *  .....//將異常轉換,只需要告知出現異常
 * }
 * void adddata()throws Nosuess
 * {
 *  1.串連資料庫
 *  try
 *  {
 *  添加資料,出現異常  SQLException,但是添加資料的人不知道SQLException是什麼
 *  }
 *  catch
 *  {
 *  內部處理
 *  throw new Nosuess();
 *  }
 *  finally
 *  {
 *  關閉資料庫
 *  }
 * }
 * */


十一、異常的注意事項

(1).子類在覆蓋父類方法時,父類的方法如果拋出了異常,那麼子類的方法只能拋出子類的異常或者該異常的子類(見下述代碼)

(2).如果父類拋出多個異常,那麼子類只能拋出父類異常的子集

簡單說:子類覆蓋父類只能拋出父類的異常或者父類異常的子類或者子集

注意:如果父類的方法沒有拋出異常,那麼子類的覆蓋時絕對不能拋


下述代碼解釋注意事項(1)

 class A extends Exception{}class B extends A{}class C extends Exception{}class FU{void show()throws A{}}class Test{void method(FU f){try{f.show();}catch (A a){}}}class ZI extends FU{void show()throws A,B //也可以不拋,但是不可以拋出其他異常{}}public class Main{public static void main(String[] args){Test t = new Test();t.method(new ZI());//用多態就可以解釋為什麼不能拋其他的異常}}

下述代碼解釋注意事項(2 )

interface in{abstract void show();}class BB implements in{public void show()//throws Exception,因為父類沒有出現異常,子類就不能拋{//如果真的出現異常,那麼只能try,不能拋}}

異常到此就結束了,內容很多,自己需要多複習,消化才是。。。



java學習注意事項

把基礎知識(CoreJav)打牢固,然後多看別人的優秀代碼,從中吸取精華,然後自己多動手做項目,從項目中學習是最快最有效地學習Java的方法
推薦Java編程思想,Effective Java,Thinking in Java
 
怎學習java

做哪個方向?網站還是android?
共同的是先學j2se,
然後j2ee的話——資料庫——tomcat,jsp——servlet+javabean——html,css,javascript,ajax——上傳——struts2——hibernate3——spring3——ejb3.0——伺服器最佳化(叢集,雲,負載平衡)
加起來的話至少要學2年+若干年的工作積累
如果你連編程都沒學
那就加上c語言/vb,資料結構,作業系統,網路,、、、、
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.