Skip to content

Commit 91a59cd

Browse files
author
turingfly
committed
Exceptions and Assertions
1 parent 8983bcb commit 91a59cd

File tree

4 files changed

+51
-49
lines changed

4 files changed

+51
-49
lines changed

Java-8/src/exceptionsAndAssertions/Assertions.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
package exceptionsAndAssertions;
22

33
/**
4-
*
54
* @author chengfeili
65
* Jun 11, 2017 11:25:30 AM
76
*
8-
* An assertion is a Boolean expression that you place at a point in
9-
* your code where you expect something to be true.
10-
*
11-
* assert boolean_expression; assert boolean_expression: error_message;
12-
*
13-
* If assertions are disabled, Java skips the assertion and goes on in
14-
* the code. If assertions are enabled and the boolean expression is
15-
* true, then our assertion has been validated and nothing happens. The
16-
* program continues to execute in its normal manner. If assertions are
17-
* enabled and the boolean expression is false, then our assertion is
18-
* invalid and a java.lang.AssertionError is thrown.
19-
*
20-
* 1. Internal Invariants You assert that a value is within a certain
21-
* constraint. assert x < 0 is an example of an internal invariant. 2.
22-
* Class Invariants You assert the validity of an object’s state. Class
23-
* invariants are typically private methods within the class that return
24-
* a boolean. The upcoming Rectangle class demonstrates a class
25-
* invariant. 3. Control Flow Invariants You assert that a line of code
26-
* you assume is unreachable is never reached. The upcoming TestSeasons
27-
* class demonstrates a control ow invariant. 4. Preconditions You
28-
* assert that certain conditions are met before a method is invoked. 5.
29-
* Post Conditions You assert that certain conditions are met after a
30-
* method executes successfully.
7+
* An assertion is a Boolean expression that you place at a point in your code
8+
* where you expect something to be true.
9+
*
10+
* assert boolean_expression; assert boolean_expression: error_message;
11+
*
12+
* If assertions are disabled, Java skips the assertion and goes on in the code.
13+
* If assertions are enabled and the boolean expression is true, then our
14+
* assertion has been validated and nothing happens. The program continues to
15+
* execute in its normal manner. If assertions are enabled and the boolean
16+
* expression is false, then our assertion is invalid and a
17+
* java.lang.AssertionError is thrown.
18+
*
19+
* 1. Internal Invariants. You assert that a value is within a certain
20+
* constraint. assert x < 0 is an example of an internal invariant.
21+
*
22+
* 2.Class Invariants. You assert the validity of an object’s state. Class
23+
* invariants are typically private methods within the class that return a
24+
* boolean.
25+
*
26+
* 3. Control Flow Invariants. You assert that a line of code you assume is
27+
* unreachable is never reached. The upcoming TestSeasons class demonstrates a
28+
* control of invariant.
29+
*
30+
* 4. Preconditions. You assert that certain conditions are met before a method
31+
* is invoked.
32+
*
33+
* 5. Post Conditions. You assert that certain conditions are met after a method
34+
* executes successfully.
3135
*/
3236
public class Assertions {
3337
public static void main(String[] args) {

Java-8/src/exceptionsAndAssertions/CallingMethodsThatThrowExceptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package exceptionsAndAssertions;
23

34
/**

Java-8/src/exceptionsAndAssertions/ExceptionIntro.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* 2. Checked Exception: includes Exception and all subclasses that
1414
* do not extend RuntimeException.
1515
* For checked exceptions, Java requires the code either handle them
16-
* ore declare them in the method signature.
16+
* or declare them in the method signature.
1717
*
1818
* 3. Error
1919
*/
@@ -50,8 +50,6 @@ public void TryStatement() {
5050
}
5151

5252
private void getUp() {
53-
// TODO Auto-generated method stub
54-
5553
}
5654

5755
public void fall1() {

Java-8/src/exceptionsAndAssertions/TryWithResources.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
*
1111
* @author chengfeili
1212
* Jun 11, 2017 11:21:42 AM
13-
*
14-
* Remember that only a try-with-resources statement is permitted to
15-
* omit both the catch and finally blocks. A traditional try statement
16-
* must have either or both.
13+
*
14+
* Remember that only a try-with-resources statement is permitted to omit both
15+
* the catch and finally blocks. A traditional try statement must have either or
16+
* both.
1717
*/
1818
public class TryWithResources {
1919
/**
2020
*
21-
* The new try-with-resources statement automatically closes all
22-
* resources opened in the try clause. This feature is also
23-
* known as automatic resource management, because Java
24-
* automatically takes care of the closing.
25-
*
26-
* The resources created in the try clause are only in scope within the try block.
21+
* The new try-with-resources statement automatically closes all resources
22+
* opened in the try clause. This feature is also known as automatic
23+
* resource management, because Java automatically takes care of the
24+
* closing.
25+
*
26+
* The resources created in the try clause are only in scope within the try
27+
* block.
2728
*/
2829
public void newApproach(Path path1, Path path2) throws IOException {
29-
try (BufferedReader in = Files.newBufferedReader(path1);
30-
BufferedWriter out = Files.newBufferedWriter(path2)) {
30+
try (BufferedReader in = Files.newBufferedReader(path1); BufferedWriter out = Files.newBufferedWriter(path2)) {
3131
out.write(in.readLine());
3232
}
3333
}
@@ -41,6 +41,7 @@ public void newApproach(Path path1, Path path2) throws IOException {
4141
*
4242
*/
4343
class TurkeyCage implements AutoCloseable {
44+
@Override
4445
public void close() {
4546
System.out.println("Close gate");
4647
}
@@ -60,11 +61,13 @@ public static void main(String[] args) {
6061
*
6162
*/
6263
class JammedTurkeyCage implements AutoCloseable {
64+
@Override
6365
public void close() throws IllegalStateException {
6466
throw new IllegalStateException("Cage door does not close");
6567
}
66-
// print
67-
// caught: turkeys ran off
68+
69+
// print
70+
// caught: turkeys ran off
6871
// Cage door does not close
6972
public static void main(String[] args) {
7073
try (JammedTurkeyCage t = new JammedTurkeyCage()) {
@@ -79,23 +82,19 @@ public static void main(String[] args) {
7982

8083
/**
8184
*
82-
* Resources are closed after the try clause ends and before any catch/finally clauses.
83-
* Resources are closed in the reverse order from which they were
85+
* Resources are closed after the try clause ends and before any catch/finally
86+
* clauses. Resources are closed in the reverse order from which they were
8487
* created.
8588
*
86-
* Print:
87-
* Close: 2
88-
* Close: 1
89-
* ex
90-
* finally
89+
* Print: Close: 2 Close: 1 ex finally
9190
*/
9291
class Auto implements AutoCloseable {
9392
int num;
9493

9594
Auto(int num) {
9695
this.num = num;
9796
}
98-
97+
@Override
9998
public void close() {
10099
System.out.println("Close: " + num);
101100
}

0 commit comments

Comments
 (0)