201521123011 《java程序设计》 第9周学习总结
1. 本周學習總結
1.1 以你喜歡的方式(思維導圖或其他)歸納總結異常相關內容。
2. 書面作業
本次PTA作業題集異常
1.常用異常
題目5-1
1.1 截圖你的提交結果(出現學號)
1.2 自己以前編寫的代碼中經常出現什么異常、需要捕獲嗎(為什么)?應如何避免?
1.3 什么樣的異常要求用戶一定要使用捕獲處理?
答:
1.1
1.2
會出現數組越界等異常,不需要捕獲,因為數組越界屬于RuntimeException,不需要捕獲。只需要修改數組長度就可以了。
1.3
Error(嚴重錯誤):無需關心
RuntimeException:無需try-catch
Exception其他子類:必須try-catch處理
2.處理異常使你的程序更加健壯
題目5-2
2.1 截圖你的提交結果(出現學號)
2.2 實驗總結
答:
2.1
2.2
在讀取字符時用了String inputInt = sc.next();所以在輸入時需要多讀取一個enter鍵,不然會無法出現排序的答案
在使用try-catch,只需要將出現異常的部分放在其中就行了。
try {int y = Integer.parseInt(inputInt);x[i] = y;i++;} catch (Exception e)3.throw與throws
題目5-3
3.1 截圖你的提交結果(出現學號)
3.2 閱讀Integer.parsetInt源代碼,結合3.1說說拋出異常時需要傳遞給調用者一些什么信息?
答:
3.1
3.2
public static int parseInt(String s, int radix)throws NumberFormatException{/** WARNING: This method may be invoked early during VM initialization* before IntegerCache is initialized. Care must be taken to not use* the valueOf method.*/if (s == null) {throw new NumberFormatException("null");}if (radix < Character.MIN_RADIX) {throw new NumberFormatException("radix " + radix +" less than Character.MIN_RADIX");}if (radix > Character.MAX_RADIX) {throw new NumberFormatException("radix " + radix +" greater than Character.MAX_RADIX");}int result = 0;boolean negative = false;int i = 0, len = s.length();int limit = -Integer.MAX_VALUE;??//limit?默認初始化為?最大正整數的??負數?,假如字符串表示的是正數,??//那么result(在返回之前一直是負數形式)就必須和這個最大正數的負數來比較,判斷是否溢出?int multmin;int digit;if (len > 0) {?//?首先是對第一個位置判斷,是否含有正負號??char firstChar = s.charAt(0);if (firstChar < '0') { // Possible leading "+" or "-"if (firstChar == '-') {negative = true;limit = Integer.MIN_VALUE;} else if (firstChar != '+')throw NumberFormatException.forInputString(s);if (len == 1) // Cannot have lone "+" or "-"throw NumberFormatException.forInputString(s);i++;}multmin = limit / radix;//?這個是用來判斷當前的?result?在接受下一個字符串位置的數字后會不會溢出。while (i < len) {// Accumulating negatively avoids surprises near MAX_VALUEdigit = Character.digit(s.charAt(i++),radix);if (digit < 0) {throw NumberFormatException.forInputString(s);}if (result < multmin) {throw NumberFormatException.forInputString(s);}result *= radix;if (result < limit + digit) {throw NumberFormatException.forInputString(s);}result -= digit;}} else {throw NumberFormatException.forInputString(s);}return negative ? result : -result;}以上源代碼告訴我們radix?要在(2~36)范圍內,不然會拋出NumberFormatException異常。Integer.parsetInt會根據不同的情況拋出異常,來告訴用戶出錯的地方以方便修改。比如5-3中輸入時begin不得小于0,end不得大于arr.length,否則拋出異常
4.函數題
題目4-1(多種異常的捕獲)
4.1 截圖你的提交結果(出現學號)
4.2 一個try塊中如果可能拋出多種異常,捕獲時需要注意些什么?
答:
4.1
4.2
catch(Exception e){System.out.println(e);}當try塊中如果可能拋出多種異常時,如果用catch一個個捕獲會很麻煩,我們可以發現很多異常都是Exception的子類,所以只需要用catch(Exception e)就可以捕捉所有的異常。
以下是通過百度找到的常見的繼承關系。
5.為如下代碼加上異常處理
byte[] content = null; FileInputStream fis = new FileInputStream("testfis.txt"); int bytesAvailabe = fis.available();//獲得該文件可用的字節數 if(bytesAvailabe>0){content = new byte[bytesAvailabe];//創建可容納文件大小的數組fis.read(content);//將文件內容讀入數組 } System.out.println(Arrays.toString(content));//打印數組內容5.1 改正代碼,讓其可正常運行。注1:里面有多個方法均可能拋出異常。注2:要使用finally關閉資源。
5.2 使用Java7中的try-with-resources來改寫上述代碼實現自動關閉資源.
答:
5.1
5.2
public static void main(String[] args) throws IOException {byte[] content = null;try(FileInputStream fis = new FileInputStream("testfis.txt");) {int bytesAvailabe = fis.available();// 獲得該文件可用的字節數if (bytesAvailabe > 0) {content = new byte[bytesAvailabe];// 創建可容納文件大小的數組fis.read(content);// 將文件內容讀入數組}System.out.println(Arrays.toString(content));// 打印數組內容} }6.重點考核:使用異常改進你的購物車系統(未提交,得分不超過6分)
舉至少兩個例子說明你是如何使用異常處理機制讓你的程序變得更健壯。
說明要包含2個部分:1. 問題說明(哪里會碰到異常)。2.解決方案(關鍵代碼)
遭遇的問題1.可能因為輸入無效數字編號到導致異常。2.也可能有文件已經結束異常、文件未找到異常。
解決問題一:
3. 碼云上代碼提交記錄
題目集:異常
3.1. 碼云代碼提交記錄
轉載于:https://www.cnblogs.com/jiaowoxiaotiancai/p/6747160.html
總結
以上是生活随笔為你收集整理的201521123011 《java程序设计》 第9周学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: test18
- 下一篇: FTL页面常用到的一些方法combobo