grep

Engineering

Java Exception 생성 비용은 비싸다.

NHN

2015년 11월 30일

원문에서 보기 ↗

java.jpg

Creating an exception in Java is very slow

at java.lang.Throwable.fillInStackTrace(Throwable.java:-1)
at java.lang.Throwable.fillInStackTrace(Throwable.java:782)
- locked <0x6c> (a sun.misc.CEStreamExhausted)
at java.lang.Throwable.<init>(Throwable.java:250)
at java.lang.Exception.<init>(Exception.java:54)
at java.io.IOException.<init>(IOException.java:47)
at sun.misc.CEStreamExhausted.<init>(CEStreamExhausted.java:30)
at sun.misc.BASE64Decoder.decodeAtom(BASE64Decoder.java:117)
at sun.misc.CharacterDecoder.decodeBuffer(CharacterDecoder.java:163)
at sun.misc.CharacterDecoder.decodeBuffer(CharacterDecoder.java:194)

*** 해결방안**

  1. Overriding fillInStackTrace method
@Override 
public synchronized Throwable fillInStackTrace() {
    return this;
}
  1. Caching an exception
public class CustomException extends RuntimeException {
    public static final CustomException INVALID_NICKNAME = new CustomException(ResponseType.INVALID_NICKNAME);
    public static final CustomException INVALID_PARAMETER = new CustomException(ResponseType.INVALID_PARAMETER);
    public static final CustomException INVALID_TOKEN = new CustomException(ResponseType.INVALID_TOKEN);
    //생략
}
if (StringUtils.isBlank(parameter)) {
    throw WebtoonCoreException.INVALID_PARAMETER;
}

exception과 더불어 logging 역시 적절하게 사용하는 것이 성능 측면에 도움이 될 것 같습니다.