接著前面的學習:
java學習筆記4--類與對象的基本概念(2)
java學習筆記3--類與對象的基本概念(1)
java學習筆記2--數(shù)據(jù)類型、數(shù)組
java學習筆記1--開發(fā)環(huán)境平臺總結
本文地址:http://m.survivalescaperooms.com/archimedes/p/java-study-note5.html,轉載請注明源地址。
1、方法的控制流程Java中的流程控制結構主要有三種:
順序結構
選擇結構
if語句(二路選擇結構)、switch語句(多路選擇結構)
循環(huán)結構
for語句、while語句、do-while語句
跑個程序:
輸入一個年份,判斷它是不是閏年。(閏年: 能被4整除但不能被100整除,或者能被400整除)public class test { public static void main(String[ ] args) throws IOException { int year; boolean IsLeapYear; System.out.由于java相關循環(huán)控制與C語言類似,所以不加贅述
2、異常處理簡介異常的基本概念:
又稱為例外,是特殊的運行錯誤對象;是面向對象規(guī)范的一部分,是異常類的對象;Java中聲明了很多異常類,每個異常類都代表了一種運行錯誤,類中包含了該運行錯誤的信息
處理錯誤的方法:
每當Java程序運行過程中發(fā)生一個可識別的運行錯誤時,即該錯誤有一個異常類與之相對應時,系統(tǒng)都會產生一個相應的該異常類的對象,即產生一個異常
異常處理示意圖:

Java異常處理機制的優(yōu)點:將錯誤處理代碼從常規(guī)代碼中分離出來
按錯誤類型和差別分組
對無法預測的錯誤的捕獲和處理
克服了傳統(tǒng)方法的錯誤信息有限的問題
把錯誤傳播給調用堆棧
程序運行過程中發(fā)生的異常事件,根據(jù)錯誤的嚴重程度不同,可分為兩類
錯誤
致命性的,用戶程序無法處理
Error類是所有錯誤類的父類
異常
非致命性的,可編制程序捕獲和處理
Exception類是所有異常類的父類
異常和錯誤類的層次結構:

Java預定義的一些常見異常:ArithmeticException:整數(shù)除法中除數(shù)為0
NullPointerException:訪問的對象還沒有實例化
NegativeArraySizeException:創(chuàng)建數(shù)組時元素個數(shù)是負數(shù)
ArrayIndexOutOfBoundsException:訪問數(shù)組元素時,數(shù)組下標越界
ArrayStoreException:程序試圖向數(shù)組中存取錯誤類型的數(shù)據(jù)
FileNotFoundException:試圖存取一個并不存在的文件
IOException:通常的I/O錯誤
異常的處理:
對于檢查型異常,Java強迫程序必須進行處理。處理方法有兩種:聲明拋出異常
不在當前方法內處理異常,而是把異常拋出到調用方法中捕獲異常,使用try{} catch() {}塊,捕獲到所發(fā)生的異常,并進行相應的處理
聲明拋出異常
如果程序員不想在當前方法內處理異常,可以使用throws子句聲明將異常拋出到調用方法中
如果所有的方法都選擇了拋出此異常,最后JVM將捕獲它,輸出相關的錯誤信息,并終止程序的運行。在異常被拋出的過程中, 任何方法都可以捕獲它并進行相應的處理
舉個例子:
public void openThisFile(String fileName) throws java.io.FileNotFoundException { //code for method }public void getCustomerInfo() throws java.io.FileNotFoundException { // do something this.openThisFile("customer.txt"); // do something }如果在openThisFile中拋出了FileNotfoundException異常,getCustomerInfo將停止執(zhí)行,并將此異常傳送給它的調用者
捕獲異常
語法格式:try { statement(s)} catch (exceptiontype name) { statement(s)} finally { statement(s)}說明
try 語句,其后跟隨可能產生異常的代碼塊
catch語句,其后跟隨異常處理語句,通常用到兩個方法
getMessage(),返回一個字符串對發(fā)生的異常進行描述。
printStackTrace(),給出方法的調用序列,一直到異常的產生位置
finally語句,不論在try代碼段是否產生異常,finally 后的程序代碼段都會被執(zhí)行。通常在這里釋放內存以外的其他資源
注意事項
在類層次樹中,一般的異常類型放在后面,特殊的放在前面
舉個例子:
import java.io.*; public class ExceptionTester { public static void main(String args[]) { System.out.println("Enter the first number:"); int number1 = Keyboard.getInteger(); System.out.println("Enter the second number:"); int number2 = Keyboard.getInteger(); System.out.print(number1 + " / " + number2 + "="); int result = number1 / number2; System.out.println(result); } }其中,Keyboard類的聲明如下:
import java.io.*; public class Keyboard{ static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static int getInteger() { try { return (Integer.valueOf(inputStream.readLine().trim()).intValue()); } catch (Exception e) { e.printStackTrace(); return 0; } } public static String getString() { try { return (inputStream.readLine()); } catch (IOException e) { return "0"; } }}運行結果:Enter the first number:
140
Enter the second number:
abc
java.lang.NumberFormatException: abc
at java.lang.Integer.parseInt(Integer.java:426)
at java.lang.Integer.valueOf(Integer.java:532)
at Keyboard.getInteger(Keyboard.java:10)
at ExceptionTester.main(ExceptionTester.java:7)
140 / 0=Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionTester.main(ExceptionTester.java:10)
3、方法的重載(overloading)一個類中名字相同的多個方法,這些方法的參數(shù)必須不同,Java可通過參數(shù)列表的不同來辨別重載的方法:或者參數(shù)個數(shù)不同、或者參數(shù)類型不同
返回值可以相同,也可以不同;重載的價值在于它允許通過使用一個方法名來訪問多個方法
舉個例子:
class MethodOverloading { public void receive(int i){ System.out.println("Receive one int parameter. "); System.out.println("i="+i); } public void receive(double d){ System.out.println("Receive one double parameter. "); System.out.println("d="+d); } public void receive(String s){ System.out.println("Receive one String parameter. "); System.out.println("s="+s); } public void receive(int i,int j){ System.out.println("Receive two int parameters. "); System.out.println("i=" + i + " j=" + j); } public void receive(int i,double d){ System.out.println("Receive one int parameter and one double parameter. "); System.out.println("i=" + i + " d=" + d); } }public class test { public static void main(String args[]){ MethodOverloading m = new MethodOverloading(); m.receive(2); m.receive(5.6); m.receive(3,4); m.receive(7,8.2); m.receive("Is it fun?"); }}運行結果:Receive one int parameter.
i=2
Receive one double parameter.
d=5.6
Receive two int parameters.
i=3 j=4
Receive one int parameter and one double parameter.
i=7 d=8.2
Receive one String parameter.
s=Is it fun?
參考資料:《java程序設計》--清華大學
新聞熱點
疑難解答