Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-105

[lang] ExceptionUtils goes into infinite loop in getThrowables is throwable.getCause() == throwable

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.1
    • 2.2
    • None
    • None
    • Operating System: Windows 2000
      Platform: PC

    • 37038

    Description

      The ExceptionUtils.getThrowables(throwable) can loop for ever if
      throwable.getCause() == throwable.

      The following code should fix this:

      public static Throwable[] getThrowables(final Throwable throwable) {
      List list = new ArrayList();

      if (throwable != null) {
      Throwable cause = throwable.getCause();
      list.add(throwable);
      while (cause != null && cause != throwable)

      { list.add(cause); cause = ExceptionUtils.getCause(cause); }

      }

      return (Throwable[]) list.toArray(new Throwable[list.size()]);
      }

      Also, the getCauseUsingWellKnownTypes(throwable) should be enhanced to avoid the
      same problems:

      private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) {
      if (throwable instanceof Nestable && throwable.getCause() != throwable)

      { return ((Nestable) throwable).getCause(); }

      else if (throwable instanceof SQLException)

      { return ((SQLException) throwable).getNextException(); }

      else if (throwable instanceof InvocationTargetException)

      { return ((InvocationTargetException) throwable).getTargetException(); }

      else

      { return null; }

      }

      Cheers

      Andy

      Attachments

        Activity

          People

            Unassigned Unassigned
            andylehane@hotmail.com Andy Lehane
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: