All Downloads are FREE. Search and download functionalities are using the official Maven repository.

devutility.internal.lang.ExceptionUtils Maven / Gradle / Ivy

There is a newer version: 1.3.8.1
Show newest version
package devutility.internal.lang;

import java.util.ArrayList;
import java.util.List;

import devutility.internal.base.SystemUtils;

public class ExceptionUtils {
	public static String toString(Exception exception) {
		return toString(exception, SystemUtils.lineSeparator());
	}

	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();
	}

	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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy