From b49128c36accec5b3a1babae5a2d09c24c3c1877 Mon Sep 17 00:00:00 2001 From: haerong22 Date: Sun, 1 May 2022 18:18:53 +0900 Subject: [PATCH] closed #8 jdbc: unchecked exception --- .../exception/basic/UncheckedAppTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 jdbc/src/test/java/com/example/jdbc/exception/basic/UncheckedAppTest.java diff --git a/jdbc/src/test/java/com/example/jdbc/exception/basic/UncheckedAppTest.java b/jdbc/src/test/java/com/example/jdbc/exception/basic/UncheckedAppTest.java new file mode 100644 index 00000000..2b330f0e --- /dev/null +++ b/jdbc/src/test/java/com/example/jdbc/exception/basic/UncheckedAppTest.java @@ -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); + } + } +}