Skip to content

Commit bfacb9c

Browse files
committed
重新编写exception一章的ppt文件
1 parent b77a221 commit bfacb9c

File tree

6 files changed

+3838
-50
lines changed

6 files changed

+3838
-50
lines changed

guide/code/exception/src/cn/edu/sdut/softlab/exception/DivTest.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,28 @@
77
*/
88
public class DivTest {
99

10-
/**
11-
* 程序执行入口.
12-
*
13-
* @param args 命令行参数
14-
*/
15-
public static void main(String[] args) {
16-
try {
17-
int a = Integer.parseInt(args[0]);
18-
int b = Integer.parseInt(args[1]);
19-
int c = a / b;
20-
System.out.println("您输入的两个数相除的结果是: " + c);
21-
22-
} catch (ArrayIndexOutOfBoundsException e) {
23-
System.out.println("数组越界,运行程序时输入的参数个数不对。应该输入2个参数,您输入的参数个数是:" + args.length);
24-
e.printStackTrace();
25-
} catch (NumberFormatException e) {
26-
System.out.println("数字格式异常,程序只能接受整数形式的参数");
27-
e.printStackTrace();
28-
} catch (ArithmeticException e) {
29-
System.out.println("数学异常,除数不能为0");
30-
e.printStackTrace();
31-
} catch (Exception e) {
32-
System.out.println("天知道发生了什么,总之情况不对");
33-
e.printStackTrace();
34-
}
35-
}
10+
/**
11+
* 程序执行入口.
12+
*
13+
* @param args 命令行参数
14+
*/
15+
public static void main(String[] args) {
16+
try {
17+
int a = Integer.parseInt(args[0]);
18+
int b = Integer.parseInt(args[1]);
19+
System.out.println("a/b = " + a / b);
20+
} catch (ArrayIndexOutOfBoundsException e) {
21+
System.out.println("数组越界,运行程序时输入的参数个数不对。应该输入2个参数,您输入的参数个数是:" + args.length);
22+
e.printStackTrace();
23+
} catch (NumberFormatException e) {
24+
System.out.println("数字格式异常,程序只能接受整数形式的参数");
25+
e.printStackTrace();
26+
} catch (ArithmeticException e) {
27+
System.out.println("数学异常,除数不能为0");
28+
e.printStackTrace();
29+
} catch (Exception e) {
30+
System.out.println("天知道发生了什么,总之情况不对");
31+
e.printStackTrace();
32+
}
33+
}
3634
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.edu.sdut.softlab.exception;
2+
3+
public class DivTestCLike {
4+
public static void main(String[] args) {
5+
if(args.length != 2){
6+
System.out.println("请提供2个参数!");
7+
return;
8+
}
9+
10+
if(args[1].equalsIgnoreCase("0")){
11+
System.out.println("除数不能为0!");
12+
return;
13+
}
14+
15+
// 判断args[0]和args[1]是整数形式的字符串
16+
if(!isIntegerStr(args[0]) || !isIntegerStr(args[1])) {
17+
System.out.println("两个参数的格式不合法,无法转换为整数");
18+
return;
19+
}
20+
21+
int a = Integer.parseInt(args[0]);
22+
int b = Integer.parseInt(args[1]);
23+
System.out.println("a/b = " + a/b);
24+
}
25+
26+
private static boolean isIntegerStr(String arg) {
27+
// TBD
28+
return true;
29+
}
30+
}

guide/code/exception/src/cn/edu/sdut/softlab/exception/DivTestNoException.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class DivTestNoException {
1515
public static void main(String[] args) {
1616
int a = Integer.parseInt(args[0]);
1717
int b = Integer.parseInt(args[1]);
18-
int c = a / b;
19-
System.out.println("您输入的两个数相除的结果是: " + c);
18+
System.out.println("a/b = " + a/b);
2019
}
2120
}
Binary file not shown.

0 commit comments

Comments
 (0)