예를 들면 다음코드를 보자.
1: Exception in thread "main" java.io.FileNotFoundException: noexitfile.txt (지정된 파일을 찾을 수 없습니다)
2: at java.io.FileInputStream.open(Native Method)
3: at java.io.FileInputStream.<init>(Unknown Source)
4: at groovy.util.CharsetToolkit.<init>(CharsetToolkit.java:68)
5: at org.codehaus.groovy.runtime.DefaultGroovyMethods.newReader(DefaultGroovyMethods.java:7499)
6: at org.codehaus.groovy.runtime.DefaultGroovyMethods.eachLine(DefaultGroovyMethods.java:6797)
7: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
8: at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
9: at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
10: at java.lang.reflect.Method.invoke(Unknown Source)
11: at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:51)
12: at org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:54)
13: at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:599)
14: at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
15: at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:740)
16: at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:761)
17: at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:749)
18: at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:167)
19: at Test.onPurposeException(Test.groovy:12)
20: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
21: at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
22: at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
23: at java.lang.reflect.Method.invoke(Unknown Source)
24: at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
25: at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:599)
26: at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
27: at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:77)
28: at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrent0(ScriptBytecodeAdapter.java:109)
29: at Test.run(Test.groovy:17)
30: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
31: at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
32: at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
33: at java.lang.reflect.Method.invoke(Unknown Source)
34: at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
35: at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:599)
36: at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
37: at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:740)
38: at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:773)
39: at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:753)
40: at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:402)
41: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
42: at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
43: at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
44: at java.lang.reflect.Method.invoke(Unknown Source)
45: at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
46: at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:599)
47: at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1077)
48: at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:744)
49: at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:167)
50: at Test.main(Test.groovy)
이렇게 필요없는 메시지를 제고하고 보고 싶다면 다음 함수를 추가해서 사용하기 바란다.
위 함수를 이용하여 try catch문을 적용해 보자.
결과는 아래와 같이 깔끔하게 나온다.
보시다시피 어느 라인에서 문제가 발생하는지 한눈에 알아볼 수 있다.
다음 버전에서는 더 좋은 기능을 기대해 보기로 하자.