View Javadoc

1   package org.jmage.util;
2   
3   /***
4    * ExceptionUtil
5    */
6   public class ExceptionUtil {
7   
8       /***
9        * Try various ways to figure out if something "is in" an Exception
10       *
11       * @param throwable Exception
12       * @return true | false
13       */
14      public boolean hasMessage(Throwable throwable, String message) {
15          if (message == null || throwable == null) {
16              return false;
17          }
18  
19          boolean hasMessage = false;
20  
21          //try on toString()
22          String stringRepresentation = throwable.toString();
23          hasMessage |= (stringRepresentation.indexOf(message) > -1);
24  
25          //try on getMessage()
26          String throwableMessage = throwable.getMessage();
27          if (throwableMessage != null) {
28              hasMessage |= (throwableMessage.indexOf(message) > -1);
29          }
30  
31          //Try on Stacktrace
32          StackTraceElement[] stackTraceElements = throwable.getStackTrace();
33          if(stackTraceElements!=null) {
34              for(int i=0;i<stackTraceElements.length;i++) {
35                  StackTraceElement element = stackTraceElements[i];
36                  hasMessage |= (element.toString().indexOf(message) > -1);
37              }
38          }
39  
40          //Try on cause
41          Throwable cause = throwable.getCause();
42          if (cause != null) {
43              if (!throwable.getClass().equals(cause.getClass())) {
44                  hasMessage |= this.hasMessage(cause, message);
45              }
46          }
47  
48          return hasMessage;
49      }
50  }