관리 메뉴

IT.FARMER

java throws bubble up 예제 본문

JAVA

java throws bubble up 예제

아이티.파머 2019. 5. 7. 16:24
반응형

예외사항을 호출한 메소드에서 처리하고자 하는경우 예외 사항을 상위로 올려준다.

 

import com.aereport.adtect.common.exception.custom.AEBaseException;
import com.aereport.adtect.common.exception.custom.APIBaseException;
import lombok.ToString;
import org.junit.Test;

/**
 * <pre>
 * Description : throws bubble up test
 *
 *
 * </pre>
 *
 * @author skan
 * @version Copyright (C) 2018 by skan. All right reserved.
 * @since 2018-11-20
 */
public class ThrowsTest {

  @Test
  public void test1() throws Exception {

    try {
      this.serviceTest();
    } catch (Exception e) {
      if (e instanceof AEBaseException) {
        // 저장 실패
        return;
      } else if (e instanceof APIBaseException) {
        System.out.println("APIBaseException");
        // API 연동 실패
        return;
      }
    }
  }

  /**
   * 서비스 로직 1
   * @throws Exception
   */
  public void serviceTest() throws Exception {

    this.serviceCommonTest2();

  }

  /**
   * 서비스 로직 2
   * @throws Exception
   */
  public void serviceCommonTest2() throws Exception {

    try {
      //TODO
    } catch (Exception e) {
      throw new AEBaseException();
    }

    try {
      // FIXME 강제 오류
      throw new APIBaseException();
    } catch (Exception e) {
      throw new APIBaseException();
    }
  }

}
반응형