Skip to content

Commit 492dc1f

Browse files
authored
format code
1 parent 6442b92 commit 492dc1f

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

ch06/03_Exception_Handling.md

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,86 @@
1010
例如,下面是一个新的异常的允许定义,它包含一个整数值:
1111

1212
```java
13-
class IntegerException extends Exception {
14-
private final int value;
15-
public IntegerException(int value) { this.value = value; }
16-
public int getValue() { return value; }
17-
}
13+
class IntegerException extends Exception {
14+
private final int value;
15+
public IntegerException(int value) {
16+
this.value = value;
17+
}
18+
public int getValue() {
19+
return value;
20+
}
21+
}
1822
```
1923

2024
这里是一个简单的例子,说明如何使用异常:
2125

2226
```java
23-
class IntegerExceptionTest {
24-
public static void main(String[] args) {
25-
try {
26-
throw new IntegerException(42);
27-
} catch (IntegerException e) {
28-
assert e.getValue() == 42;
29-
}
30-
}
27+
class IntegerExceptionTest {
28+
public static void main(String[] args) {
29+
try {
30+
throw new IntegerException(42);
31+
} catch (IntegerException e) {
32+
assert e.getValue() == 42;
3133
}
34+
}
35+
}
3236
```
3337

3438
`try` 语句的主体用 `catch` 语句捕获的给定值抛出异常。
3539

3640
相反,以下定义的新异常是禁止的,因为它创建了一个参数化类型:
3741

3842
```java
39-
class ParametricException<T> extends Exception { // 编译报错
40-
private final T value;
41-
public ParametricException(T value) { this.value = value; }
42-
public T getValue() { return value; }
43-
}
43+
class ParametricException<T> extends Exception { // 编译报错
44+
private final T value;
45+
public ParametricException(T value) {
46+
this.value = value;
47+
}
48+
public T getValue() {
49+
return value;
50+
}
51+
}
4452
```
4553

4654
试图编译上述报告错误:
4755

4856
```java
49-
% javac ParametricException.java
50-
ParametricException.java:1: a generic class may not extend
51-
java.lang.Throwable
52-
class ParametricException<T> extends Exception { // 编译报错
53-
^
54-
1 error
57+
% javac ParametricException.java
58+
ParametricException.java:1: a generic class may not extend
59+
java.lang.Throwable
60+
class ParametricException<T> extends Exception { // 编译报错
61+
^
62+
1 error
5563
```
5664

5765
这种限制是明智的,因为几乎任何捕捉这种异常的尝试都必须失败,因为该类型不可确定。 人们可能会期望典型的例外使用如下所示:
5866

5967
```java
60-
class ParametricExceptionTest {
61-
public static void main(String[] args) {
62-
try {
63-
throw new ParametricException<Integer>(42);
64-
} catch (ParametricException<Integer> e) { // compile-time error
65-
assert e.getValue()==42;
66-
}
67-
}
68-
}
68+
class ParametricExceptionTest {
69+
public static void main(String[] args) {
70+
try {
71+
throw new ParametricException<Integer>(42);
72+
} catch (ParametricException<Integer> e) { // compile-time error
73+
assert e.getValue()==42;
74+
}
75+
}
76+
}
6977
```
7078

7179
这是不允许的,因为 `catch` 子句中的类型是不可确定的。 在撰写本文时,`Sun` 编译器在这种情况下报告了一系列语法错误:
7280

7381
```java
74-
% javac ParametricExceptionTest.java
75-
ParametricExceptionTest.java:5: <identifier> expected
76-
} catch (ParametricException<Integer> e) {
77-
^
78-
ParametricExceptionTest.java:8: ')' expected
79-
}
80-
^
81-
ParametricExceptionTest.java:9: '}' expected
82-
}
83-
^
84-
3 errors
82+
% javac ParametricExceptionTest.java
83+
ParametricExceptionTest.java:5: <identifier> expected
84+
} catch (ParametricException<Integer> e) {
85+
^
86+
ParametricExceptionTest.java:8: ')' expected
87+
}
88+
^
89+
ParametricExceptionTest.java:9: '}' expected
90+
}
91+
^
92+
3 errors
8593
```
8694

8795
由于异常不能是参数化的,因此语法受到限制,因此必须将该类型编写为标识符,而没有以下参数。

0 commit comments

Comments
 (0)