Skip to content

Commit 8a25535

Browse files
committed
Merge branch 'master' of github.com:subaochen/java-tutorial
2 parents 8bd5432 + 1465460 commit 8a25535

File tree

7 files changed

+4807
-110
lines changed

7 files changed

+4807
-110
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
}

guide/exception.lyx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ reference "fig:抛出异常和捕获异常的关系"
16181618
placement tbph
16191619
wide false
16201620
sideways false
1621-
status open
1621+
status collapsed
16221622

16231623
\begin_layout Plain Layout
16241624
\align center
@@ -1657,6 +1657,29 @@ name "fig:抛出异常和捕获异常的关系"
16571657
\end_inset
16581658

16591659

1660+
\end_layout
1661+
1662+
\begin_layout Standard
1663+
\begin_inset Flex Tip
1664+
status open
1665+
1666+
\begin_layout Plain Layout
1667+
翻一下JDK的源代码,也许有助于深刻理解Java的异常处理机制。比如
1668+
\begin_inset Note Note
1669+
status open
1670+
1671+
\begin_layout Plain Layout
1672+
这里寻找一个合适易懂的Java源代码,在:http://hg.openjdk.java.net/jdk10/jdk10/jdk/file/777356696811
1673+
\end_layout
1674+
1675+
\end_inset
1676+
1677+
1678+
\end_layout
1679+
1680+
\end_inset
1681+
1682+
16601683
\end_layout
16611684

16621685
\begin_layout Exercise
Binary file not shown.

0 commit comments

Comments
 (0)