Skip to content

Commit 809a6fd

Browse files
committed
fmt
1 parent dbb5bfc commit 809a6fd

File tree

5 files changed

+124
-99
lines changed

5 files changed

+124
-99
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package syntax.tryblock;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author Kuangcp
7+
* 2025-03-12 18:22
8+
*/
9+
public class FinallyTest {
10+
11+
12+
/**
13+
* 将 输出 try 和 finally
14+
*/
15+
@Test
16+
public void testExceptionFinally() throws Exception {
17+
try {
18+
System.out.println("try");
19+
throw new RuntimeException("error");
20+
} finally {
21+
System.out.println("finally");
22+
}
23+
}
24+
}

class/src/test/java/syntax/tryblock/FinallyWithReturn.java

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package syntax.tryblock;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
import java.util.concurrent.ThreadLocalRandom;
8+
9+
/**
10+
* created by https://gitee.com/gin9
11+
* <p>
12+
* finally block should not contain return, the ide warn you as same
13+
* execution sequence: expression in return then cache in stack, execution finally, return(the return on finally cover the try)
14+
*
15+
* @author kuangcp on 2/17/19-9:24 AM
16+
*/
17+
@Slf4j
18+
public class FinallyWithReturnTest {
19+
20+
@Test
21+
public void testFinallyWithReturn() {
22+
int value = doSomethingWithCoverException();
23+
log.info("actual value: value={}", value);
24+
}
25+
26+
@Test
27+
public void testFinallyWithoutReturn() {
28+
int value = doSomething();
29+
log.info("actual value: value={}", value);
30+
}
31+
32+
// 1. finally block will override try block return value
33+
// 2. may cover Exception, if not catch corresponding exception
34+
private int doSomethingWithCoverException() {
35+
try {
36+
return alwaysZero();
37+
} finally {
38+
log.info("finally block");
39+
// if use return, even the compile-time exception will ignored
40+
// this return will cover the return on try block
41+
return 1;
42+
}
43+
}
44+
45+
// correct way
46+
private int doSomething() {
47+
try {
48+
return alwaysZero();
49+
} catch (IOException e) {
50+
log.warn("catch exception ", e);
51+
} finally {
52+
log.info("finally block");
53+
}
54+
return 1;
55+
}
56+
57+
private int alwaysZero() throws IOException {
58+
log.info("invoke alwaysZero method");
59+
if (ThreadLocalRandom.current().nextBoolean()) {
60+
log.warn("throw Exception");
61+
throw new IOException("io exception");
62+
}
63+
64+
return 0;
65+
}
66+
}

class/src/test/java/syntax/tryblock/TWR.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package syntax.tryblock;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* created by https://gitee.com/gin9
9+
* <p>
10+
* try-with-resources
11+
* as long as the object implements the AutoCloseable and Closeable interface
12+
*
13+
* @author kuangcp on 18-10-1-下午9:59
14+
*/
15+
public class TryWithResourceTest {
16+
17+
// primitive way
18+
static String ReadFile(String file) throws IOException {
19+
BufferedReader br = new BufferedReader(new FileReader(file));
20+
try {
21+
return br.readLine();
22+
} finally {
23+
br.close();
24+
}
25+
}
26+
27+
// use TWR
28+
static String ReadFileWithTWR(String file) throws IOException {
29+
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
30+
return br.readLine();
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)