Java Throwable to String stackTraceAsString(final Throwable aError)

Here you can find the source of stackTraceAsString(final Throwable aError)

Description

Extracts the given errors stack trace as a string.

License

Open Source License

Parameter

Parameter Description
aError The error to process.

Return

A string form of the stack trace.

Declaration

public static String stackTraceAsString(final Throwable aError) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.PrintWriter;
import java.io.StringWriter;

import java.util.Arrays;
import java.util.Collection;

public class Main {
    /**/*from ww  w  .  jav a 2s .  c  om*/
     * {@value}
     */
    private static final String ERROR_BAD_MSG = "Error message cannot be null or empty";
    /**
     * {@value}
     */
    public static final String ERROR_EXCEPTION_ARG_NULL = "Error argument cannot be null";
    /**
     * {@value}
     */
    public static final String ERROR_VALUE_NULL = "Value is null";

    /**
     * Extracts the given errors stack trace as a string.
     * 
     * @param aError
     *            The error to process.
     * @return A string form of the stack trace.
     */
    public static String stackTraceAsString(final Throwable aError) {

        checkNull(aError, ERROR_EXCEPTION_ARG_NULL);
        StringWriter sw = new StringWriter();
        PrintWriter w = new PrintWriter(sw);

        aError.printStackTrace(w);
        w.flush();
        w.close();

        return sw.toString();
    }

    /**
     * Checks the given collection to see if it or any of it's values are null.
     * 
     * @param values
     *            collection to check.
     * 
     * @throws IllegalArgumentException
     *             The given collection is null or there are null values.
     */
    public static void checkNull(final Collection<?> values) {
        checkNull(values, ERROR_VALUE_NULL);
    }

    /**
     * Checks the given collection to see if it or any of it's values are null.
     * 
     * @param values
     *            collection to check.
     * @param errorMessage
     *            The error message to use. The index of the null is appended to
     *            the message.
     * @throws IllegalArgumentException
     *             The given collection is null or there are null values.
     */
    public static void checkNull(final Collection<?> values, String errorMessage) {

        if (isNullOrEmpty(errorMessage)) {
            throw new IllegalArgumentException(ERROR_BAD_MSG);
        }

        if (null == values) {
            throw new IllegalArgumentException(errorMessage);
        }

        errorMessage += " at index ";

        int i = 0;

        for (Object value : values) {
            if (null == value) {
                throw new IllegalArgumentException(errorMessage + i);
            }
            i++;
        }
    }

    /**
     * Checks the given object and throws and exception if it is null.
     * 
     * @param value
     *            the value to check.
     * 
     * @throws IllegalArgumentException
     *             The value was null.
     */
    public static void checkNull(final Object value) {
        checkNull(value, ERROR_VALUE_NULL);
    }

    /**
     * Checks the given object and throws and exception if it is null.
     * 
     * @param value
     *            the value to check.
     * @param errorMessage
     *            Message to use . Cannot be null or empty.
     * @throws IllegalArgumentException
     *             The value was null.
     */
    public static void checkNull(final Object value, final String errorMessage) {

        if (isNullOrEmpty(errorMessage)) {
            throw new IllegalArgumentException(ERROR_BAD_MSG);
        }

        if (null == value) {
            throw new IllegalArgumentException(errorMessage);
        }
    }

    /**
     * Checks the given array to see if it or any of it's values are null.
     * 
     * @param values
     *            Array to check.
     * 
     * @throws IllegalArgumentException
     *             The given array is null or there are null values.
     */
    public static void checkNull(final Object[] values) {
        checkNull(values, ERROR_VALUE_NULL);
    }

    /**
     * Checks the given array to see if it or any of it's values are null.
     * 
     * @param values
     *            Array to check.
     * @param errorMessage
     *            The error message to use. The index of the null is appended to
     *            the message.
     * @throws IllegalArgumentException
     *             The given array is null or the length is zero.
     */
    public static void checkNull(final Object[] values, final String errorMessage) {

        if (isNullOrEmpty(errorMessage)) {
            throw new IllegalArgumentException(ERROR_BAD_MSG);
        }

        if (null == values) {
            throw new IllegalArgumentException(errorMessage);
        }

        checkNull(Arrays.asList(values), errorMessage);
    }

    /**
     * Tests given value to see if it is empty.
     * 
     * @param value
     *            Value to test.
     * @return True if empty.
     */
    public static boolean isNullOrEmpty(final Collection<?> value) {

        return isNull(value) || isEmpty(value);
    }

    /**
     * Tests given value to see if it is empty.
     * 
     * @param value
     *            Value to test.
     * @return True if empty.
     */
    public static boolean isNullOrEmpty(final Object[] value) {

        return isNull(value) || isEmpty(value);
    }

    /**
     * Tests given value to see if it is empty.
     * 
     * @param value
     *            Value to test.
     * @return True if empty.
     */
    public static boolean isNullOrEmpty(final String value) {

        return isNull(value) || isEmpty(value);
    }

    /**
     * Method to check for null. This is here just for completeness.
     * 
     * @param arg
     *            Any object.
     * @return True if null false if not.
     */
    public static boolean isNull(final Object arg) {

        return null == arg;
    }

    /**
     * Tests given value to see if it is empty.
     * 
     * @param value
     *            Value to test. Does not check to see if it is null.
     * @return True if empty.
     */
    public static boolean isEmpty(final Collection<?> value) {

        return 0 == value.size();
    }

    /**
     * Tests given value to see if it is empty.
     * 
     * @param value
     *            Value to test. Does not check to see if it is null.
     * @return True if empty.
     */
    public static boolean isEmpty(final Object[] value) {

        return 0 == value.length;
    }

    /**
     * Tests given value to see if it is empty.
     * 
     * @param value
     *            Value to test. Does not check to see if it is null.
     * @return True if empty.
     */
    public static boolean isEmpty(final String value) {

        return 0 == value.length();
    }
}

Related

  1. stackTrace(Throwable t)
  2. stackTrace(Throwable t)
  3. stackTrace(Throwable th)
  4. stackTrace(Throwable th, String pattern)
  5. stackTrace(Throwable throwable)
  6. stackTraceAsString(Throwable e)
  7. stackTraceAsString(Throwable throwable)
  8. stacktraceError(Throwable e)
  9. stackTraceFor(final Throwable exception)