File tree Expand file tree Collapse file tree 7 files changed +4807
-110
lines changed
code/exception/src/cn/edu/sdut/softlab/exception Expand file tree Collapse file tree 7 files changed +4807
-110
lines changed Original file line number Diff line number Diff line change 7
7
*/
8
8
public class DivTest {
9
9
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
+ }
36
34
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ public class DivTestNoException {
15
15
public static void main (String [] args ) {
16
16
int a = Integer .parseInt (args [0 ]);
17
17
int b = Integer .parseInt (args [1 ]);
18
- int c = a / b ;
19
- System .out .println ("您输入的两个数相除的结果是: " + c );
18
+ System .out .println ("a/b = " + a /b );
20
19
}
21
20
}
Original file line number Diff line number Diff line change @@ -1618,7 +1618,7 @@ reference "fig:抛出异常和捕获异常的关系"
1618
1618
placement tbph
1619
1619
wide false
1620
1620
sideways false
1621
- status open
1621
+ status collapsed
1622
1622
1623
1623
\begin_layout Plain Layout
1624
1624
\align center
@@ -1657,6 +1657,29 @@ name "fig:抛出异常和捕获异常的关系"
1657
1657
\end_inset
1658
1658
1659
1659
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
+
1660
1683
\end_layout
1661
1684
1662
1685
\begin_layout Exercise
You can’t perform that action at this time.
0 commit comments