CharSequence (@NotNull Function<? super Throwable, String> exceptionProcessor, @Nullable Consumer<? super String> stringProcessor) { if (exceptions.size() == 1) { Throwable throwable = exceptions.get(0); String s = exceptionProcessor.apply(throwable); if (stringProcessor != null) { stringProcessor.accept(s); } return s; } StringBuilder sb = new StringBuilder(); String line = "CompositeException (" + exceptions.size() + " nested):\n------------------------------\n"; if (stringProcessor != null) { stringProcessor.accept(line); } sb.append(line); for (int i = 0; i < exceptions.size(); i++) { Throwable exception = exceptions.get(i); line = "[" + (i + 1) + "]: "; if (stringProcessor != null) { stringProcessor.accept(line); } sb.append(line); line = exceptionProcessor.apply(exception); if (line == null) { line = "null\n"; } else if (!line.endsWith("\n")) { line += '\n'; } if (stringProcessor != null) { stringProcessor.accept(line); } sb.append(line); } line = "------------------------------\n"; if (stringProcessor != null) { stringProcessor.accept(line); } sb.append(line); return sb; }