closed #8 jdbc: unchecked exception
This commit is contained in:
@@ -0,0 +1,82 @@
|
|||||||
|
package com.example.jdbc.exception.basic;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class UncheckedAppTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void unchecked() {
|
||||||
|
Controller controller = new Controller();
|
||||||
|
|
||||||
|
Assertions.assertThatThrownBy(controller::request)
|
||||||
|
.isInstanceOf(Exception.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void printEx() {
|
||||||
|
Controller controller = new Controller();
|
||||||
|
try {
|
||||||
|
controller.request();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("ex", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Controller {
|
||||||
|
Service service = new Service();
|
||||||
|
|
||||||
|
public void request() {
|
||||||
|
service.logic();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Service {
|
||||||
|
Repository repository = new Repository();
|
||||||
|
NetworkClient networkClient = new NetworkClient();
|
||||||
|
|
||||||
|
public void logic() {
|
||||||
|
repository.call();
|
||||||
|
networkClient.call();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class NetworkClient {
|
||||||
|
public void call() {
|
||||||
|
throw new RuntimeConnectException("연결 실패");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Repository {
|
||||||
|
public void call() {
|
||||||
|
try {
|
||||||
|
runSQL();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeSQLException(e); // 예외를 전환할 때는 반드시 이전 예외를 전달해 주어야 한다.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runSQL () throws SQLException {
|
||||||
|
throw new SQLException("ex");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class RuntimeConnectException extends RuntimeException {
|
||||||
|
public RuntimeConnectException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class RuntimeSQLException extends RuntimeException {
|
||||||
|
public RuntimeSQLException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public RuntimeSQLException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user