본문 바로가기

Dev.../플밍 관련 자료

[펌] Exceptional practices, Part 2

Exceptional practices, Part 2

Use exception chaining to preserve debugging information

 

original resource : http://www.javaworld.com/javaworld/jw-09-2001/jw-0914-exceptions.html

 

exception chaining (sometimes called exception wrapping),
- exception을 다른 type의 exception으로의 mapping, 중요한 debugging 정보를 잃어버리지 않음.

 

What's an exception?

exception은 세가지 중요한 정보를 가지고 있다.
1. The type of exception -- the exception class
2. Where the exception occurred -- the stack trace
3. Context and explanatory information -- the error message, and other state information

1 -> software : exception을 어떻게 처리할 것인지를 결정하기 위하여 필요하다.
2, 3 -> people : developer나 engineer가 program을 debug 하거나 user나 developer가 error message를 보기
위하여 필요하다.


A first attempt at wrapping exceptions

exception을 즉시 다른 type으로 rethrow

 

  public loadResource(String resourceName) throws ResourceLoadException {
    Resource r;
    try {
      r = loadResourceFromDB(resourceName);
    }
    catch (SQLException e) {
      throw new ResourceLoadException("SQL Exception loading resource "
                                      + resourceName: " + e.toString());
    }
  }
}


Don't forget the stack trace

?첸 loadResource 에 에러가 있다면 stack trace가 사라져버렸기 때문에 에러의 위치를 결정할 수 없다.


The ChainedException class


public class ChainedException extends Exception {
  private Throwable cause = null;

  public ChainedException() {
    super();
  }

  public ChainedException(String message) {
    super(message);
  }

  public ChainedException(String message, Throwable cause) {
    super(message);
    this.cause = cause;
  }

  public Throwable getCause() {
    return cause;
  }

  public void printStackTrace() {
    super.printStackTrace();
    if (cause != null) {
      System.err.println("Caused by:");
      cause.printStackTrace();
    }
  }

  public void printStackTrace(java.io.PrintStream ps) {
    super.printStackTrace(ps);
    if (cause != null) {
      ps.println("Caused by:");
      cause.printStackTrace(ps);
    }
  }

  public void printStackTrace(java.io.PrintWriter pw) {
    super.printStackTrace(pw);
    if (cause != null) {
      pw.println("Caused by:");
      cause.printStackTrace(pw);
    }
  }
}


Integrating ChainedException into our example

ResourceLoadException를  ChainedException을 이용하여 바꾸어 보자.

 

public class ResourceLoadException extends ChainedException {
  public ResourceLoadException(String message) {
    super(message);
  }

  public ResourceLoadException(String message, Throwable cause) {
    super(message, cause);
  }
}

 

public class ResourceLoader {
  public loadResource(String resourceName) throws ResourceLoadException {
    Resource r;
    try {
      r = loadResourceFromDB(resourceName);
    }
    catch (SQLException e) {
      throw new ResourceLoadException("Unable to load resource "
                                      + resourceName, e);
    }
  }
}