本文介紹的是利用java語言實現一個控制臺版的模擬借書系統,在開始本文的正式內容之前,我們先來了解一下java異常機制。
什么是異常?
異常,不正常也。exception是exception event的縮寫,因此異常是一個事件,該事件發生在程序運行時。
異常會影響程序的連續性,使程序中斷。在java中,一切皆對象,所以要定義異常,也需要使用對象。異常對象里
封裝了異常類型和程序發生異常時的狀態。
我們經常說的拋出異常就是創建異常對象,并提交給運行系統。
異常捕獲機制與try-catch
當異常發生時,我們需要知道異常在哪里發生的,那么怎么定位異常的出處呢?
在java中,使用call stack來記錄方法調用順序。當java程序發生異常時,會搜索call stack,希望找到特定
的代碼塊來處理它。就像生病(exception)了,我們會去醫院找特定科目的醫生就診。以下圖為例,main方法
調用method0,同理method0調用method1,等等。
在java中異常處理,使用try-catch語句。語法為:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
try { // 可能出現異常的代碼 } catch (exceptiontype name) { // 異常處理代碼 } catch (exceptiontype name) { // 異常處理代碼 } |
好了,下面開始我們的正文。
實現目標
用java語言實現一個控制臺版的模擬借書系統,旨在練習java異常機制。
實現要求
實現成品
參考代碼及注釋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package com.jinger; import java.util.scanner; //導入scanner包 public class bookmanagereasy { private static scanner console = new scanner(system.in); //接收系統輸入 public static void main(string[] args) { //定義”圖書“數組 string[] books = { "c語言" , "數據結構" , "匯編語言" , "高數" , "大學語文" , "毛概" }; while ( true ) { system.out.println( "輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書" ); string book; try { //取得整型命令 int command = inputcommand(); //根據不同命令值,進行不同操作 switch (command) { case 1 : //按照圖書名稱選擇圖書 book = getbookbyname(books); system.out.println( "book:" + book); break ; case 2 : //按照圖書序號(數組下標)選擇圖書 book = getbookbynumber(books); system.out.println( "book:" + book); break ; case - 1 : //返回值為-1,說明輸入有誤 system.out.println( "命令輸入錯誤!請根據提示輸入數字命令!" ); continue ; default : //其他值的命令均認為是錯誤命令 system.out.println( "命令輸入錯誤!" ); continue ; } break ; //退出程序 } catch (exception bne) { //捕獲“圖書不存在異常”時,要求重新輸入命令 system.out.println(bne.getmessage()); continue ; } } } //按照圖書名稱查詢圖書 private static string getbookbyname(string[] books) throws exception { system.out.println( "輸入圖書名稱:" ); //獲取輸入的圖書名稱 string name = console.next(); for ( int i = 0 ; i < books.length; i++) { if (name.equals(books[i])) //輸入的名稱與某一圖書名稱匹配,返回該圖書 return books[i]; } //若無匹配,拋出”圖書不存在異常“ throw new exception( "圖書不存在!" ); } //根據圖書序號(數組下標)查詢圖書 private static string getbookbynumber(string[] books) throws exception { while ( true ) { system.out.println( "輸入圖書序號:" ); try { //獲取輸入的圖書序號(數組下標) int index = inputcommand(); //若返回值為-1 if (index == - 1 ){ system.out.println( "命令輸入錯誤!請根據提示輸入數字命令!" ); continue ; } //若不出現“數組下標越界異常”,則返回相應位置的圖書 string book = books[index]; return book; } catch (arrayindexoutofboundsexception e) { //輸入的序號不存在(引發“數組下標越界異常”),則拋出“圖書不存在異常” exception booknotexists = new exception( "圖書不存在!" ); booknotexists.initcause(e); throw booknotexists; } } } //從控制臺輸入命令,用于輸入命令和輸入圖書序號 private static int inputcommand(){ int command; try { command = console.nextint(); return command; } catch (exception e) { //若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出“錯誤命令異常” console = new scanner(system.in); //返回-1 return - 1 ; } } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://hubojing.me/2017/03/19/模擬借書系統(java異常機制實現)/