java服務(wù)器端編程基礎(chǔ)入門_第1頁
java服務(wù)器端編程基礎(chǔ)入門_第2頁
java服務(wù)器端編程基礎(chǔ)入門_第3頁
java服務(wù)器端編程基礎(chǔ)入門_第4頁
java服務(wù)器端編程基礎(chǔ)入門_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、import java.awt.*;import java.awt.event.*;import .*;import java.io.*;public class ChatServer public static void main(String args) boolean started = false; / 判斷服務(wù)器是否啟動了,先假定沒有啟動ServerSocket ss = null;DataInputStream dis = null;try ss = new ServerSocket(8888);started = true; / 服務(wù)器已經(jīng)啟動了,設(shè)置started為truewh

2、ile (started) /當(dāng)服務(wù)器端啟動時。boolean bConnect = false; /判斷服務(wù)器和客戶端的連接是否已經(jīng)建立, 沒有為false,連接成功返回trueSocket s = ss.accept(); /accept()與readUTF()方法一樣,都是一個阻塞式方法,如果沒有收到連接請求,則一直等待。bConnect = true; /連接成功,設(shè)置bConnect為trueSystem.out.println(一個連接已經(jīng)建立!); / -dis = new DataInputStream(s.getInputStream();while (bConnect) S

3、tring str = null;str = dis.readUTF(); /如果客戶端突然斷開連接,該語句就會拋出異常EOFException,所以我們必須得對dis.readUTF();進(jìn)行異常處理/readUTF()是阻塞式方法,如果得不到數(shù)據(jù),則繼續(xù)等待,直到讀取到數(shù)據(jù)為止System.out.println(從客戶端接受的數(shù)據(jù)是: + str);catch (BindException e)System.out.println(端口已被占用,其使用其他端口);System.out.println(請關(guān)閉相關(guān)程序,重新運行!);System.exit(-1);catch (EOFExc

4、eption e)System.out.println(客戶端已經(jīng)斷開連接!);catch (IOException e) /e.printStackTrace();System.out.println(服務(wù)器端讀寫錯誤!);finallytryif (null != ss) /如果監(jiān)聽程序打開了,則關(guān)閉網(wǎng)絡(luò)監(jiān)聽程序ss.close();ss = null;if (null != dis) /如果輸入流打開了,則關(guān)閉輸入流dis.close();dis = null;catch (Exception e)class Aint divide(int a, int b)int m = 0;trym

5、 = a/b;catch (ArithmeticException e)System.out.printf(除數(shù)不能為零!n);/System.out.printf(1111);return m;public class TestExcep_1public static void main(String args)new A().divide(6, 2);/System.out.printf(2222);public class TestExcep_2public static void main(String args)int m = 99;trym = 2;System.out.print

6、f(m = %dn, m);catch (Exception e)System.out.printf(m = %dn, m);import java.util.Scanner;import java.util.InputMismatchException;public class TestExcep_3public static void main(String args)int i;Scanner sc = new Scanner(System.in); /System.in 表示鍵盤tryi = sc.nextInt();System.out.printf(i = %dn, i);catc

7、h (InputMismatchException e)System.out.printf(輸入數(shù)據(jù)不合法,程序被終止!n);class Aint divide(int a, int b)return a/b;public void f()g();public void g()divide(6, 0);public class TestExcep_4public static void main(String args)trynew A().f();catch (Exception e)e.printStackTrace();import java.io.*;class Apublic voi

8、d f() throws IOException throw new IOException(); /throw 拋出異常public void g()throw new ArithmeticException();public class TestExcep_5public static void main(String args) /throws IOExceptionA aa = new A();tryaa.f();catch (IOException e)class Aint divide(int a, int b)int m = 0;m = a / b;return m;public

9、 class TestExcep_6public static void main(String args)trynew A().divide(6, 0);catch (Exception e)System.out.printf(嘿嘿n);e.printStackTrace();finallySystem.out.printf(好久啊哈n);import java.io.*;class DivisorIsZeroException extends Exceptionpublic DivisorIsZeroException(String name)super(name);class Apubl

10、ic int divide(int a, int b) throws DivisorIsZeroExceptionint m = 0;if (0 = b)throw new DivisorIsZeroException(除數(shù)不能為零! O(_)O哈哈);elsem = a/b;return m;public class TestExcep_7public static void main(String args) /throws IOExceptionA aa = new A();tryaa.divide(6, 0);catch (Exception e)e.printStackTrace()

11、;import java.io.*;class Apublic void f() throws ArithmeticException /throw new ArithmeticException(); /throw 拋出異常public class TestExcep_8public static void main(String args) /throws IOExceptionA aa = new A();tryaa.f();catch (ArithmeticException e)System.out.printf(haha );class Personpublic int age;p

12、ublic class TestNullPointerExceptionpublic static void main(String args) Person p = null;System.out.println(p.age);class DivisorIsZeroException extends Exceptionpublic DivisorIsZeroException(String errorMessage)super(errorMessage);class Aint divide(int a, int b) throws DivisorIsZeroException/try/if

13、(0 = b)/throw new DivisorIsZeroException(除數(shù)不能為零!);/catch (DivisorIsZeroException e)/e.printStackTrace();/if (0 = b)throw new DivisorIsZeroException(除數(shù)不能為零!);int m = a / b;return m;public class TestApublic static void main(String args)A aa = new A();/aa.divide(6, 2);/*2009年1月21日21:55:05子類覆蓋了基類方法時,子類方

14、法拋出異常的范圍不能大于基類方法拋出的異常范圍子類方法可以不拋出異常,也可以只拋出基類方法的部分異常但不可以拋出基類方法以外的異常*/自定義異常Aclass A extends Exception/自定義異常Bclass B extends Exception/自定義異常Cclass C extends Exceptionclass Mvoid f() throws A, Bclass N extends Mvoid f() throws A,B /可以throws A或B,也可以throws A,B 也可以不throws,但不可以throws C 即子類覆蓋了基類方法時,子類方法拋出異常的范

15、圍不能大于基類方法拋出的異常范圍class Testpublic void k(M mm)trymm.f();catch (A aa)catch (B bb)class TestExtendExcepublic static void main(String args)M m = new M();N n = new N();/System.out.println(1111);/*在JDK 1.6中的運行結(jié)果是:-1111-*/class A public String toString()return 哈哈;public class TestObjectpublic static void m

16、ain(String args)A aa = new A();System.out.printf(%sn, aa.toString();class Dianpublic int x, y;public Dian(int x, int y)this.x = x; this.y = y;public String toString()return + x + , + y + ; /3, 5public class TestPointpublic static void main(String args)Dian d = new Dian(3, 5);/System.out.printf(%s, d

17、);System.out.println(d);class Apublic void f()System.out.printf(AAAAn);class B extends Apublic void f()System.out.printf(BBBBn);public void g()public class TestPoly_1public static void main(String args)A aa = new A();B bb = new B();/aa = bb;aa.f();B bb2 = (B)aa;class Apublic int i;public A(int i)thi

18、s.i = i;/public boolean equals(Object obj)/A aa = (A)obj;/if(this.i = aa.i) /if (當(dāng)前對象的i和obj代表的i相等)/return true;/else/return false;/public class TestStringEquals_2public static void main(String args)A aa1 = new A(2);A aa2 = new A(2);System.out.println( aa1.equals(aa2) ); /true/*2009年3月14日9:34:11先catch子類異常再catch父類異常如果先catch父類異常再catch子類異常,則編譯時會報錯*/class A extends Exceptionclass B ex

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論