devutility.internal.lang.ExceptionHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of devutility.internal Show documentation
Show all versions of devutility.internal Show documentation
Utilities for Java development
package devutility.internal.lang;
import java.util.ArrayList;
import java.util.List;
import devutility.internal.system.SystemHelper;
public class ExceptionHelper {
// region to string
public static String toString(Exception exception) {
return toString(exception, SystemHelper.getNewLineChar());
}
public static String toString(Exception exception, String separator) {
StringBuilder result = new StringBuilder();
List list = toList(exception);
for (int i = 0; i < list.size(); i++) {
result.append(list.get(i));
if (i < list.size() - 1) {
result.append(separator);
}
}
return result.toString();
}
// endregion
// region to list
public static List toList(Exception exception) {
List list = new ArrayList<>();
if (exception == null) {
return list;
}
list.add(String.format("%s: %s", exception.getClass().getName(), exception.getMessage()));
StackTraceElement[] stackTraceElements = exception.getStackTrace();
if (stackTraceElements == null || stackTraceElements.length == 0) {
return list;
}
for (StackTraceElement stackTraceElement : stackTraceElements) {
list.add(String.format(" at %s", stackTraceElement.toString()));
}
return list;
}
// endregion
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy