Java Utililty Methods Exception to String

List of utility methods to do Exception to String

Description

The list of methods to do Exception to String are organized into topic(s).

Method

StringexceptionToString(Throwable e)
exception To String
StringBuilder sb = new StringBuilder(128);
StackTraceElement elements[] = e.getStackTrace();
sb.append(e.toString());
sb.append("\n");
for (int n = 0; n < elements.length; ++n) {
    sb.append("        at ");
    sb.append(elements[n].getClassName());
    sb.append(".");
...
StringexceptionToString(Throwable ex)
exception To String
return exceptionToString(ex, "");
StringexceptionToString(Throwable exception)
Converts given exception to String, including file name and line number
StringBuffer str = new StringBuffer();
str.append(exception.toString());
if (exception.getStackTrace().length > 0) {
    StackTraceElement location = exception.getStackTrace()[0];
    str.append(" (");
    str.append(location.getFileName());
    str.append(":");
    str.append(location.getLineNumber());
...
StringexceptionTypeAndMsg(Exception e)
Displays both the exception class and the message.
return e.getClass() + " : " + e.getMessage();
TexceptionWithCause(T exception, Throwable cause)
Throw the exception with the specified cause.
exception.initCause(cause);
return exception;
StringgetErrorMessage(Exception ex)
Utility method to send the error back to UI
return "Error:" + ex.toString();
StringgetErrorMessage(Exception ex)
get Error Message
StringWriter stringWriter = new StringWriter();
ex.printStackTrace(new PrintWriter(stringWriter));
String returnStr = new String(stringWriter.getBuffer());
stringWriter.flush();
try {
    stringWriter.close();
} catch (IOException e) {
    e.printStackTrace();
...
StringgetErrorMessage(Process process)
get Error Message
if (process == null) {
    return "";
BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuffer sb = new StringBuffer();
String str = br.readLine();
while (str != null) {
    str = br.readLine();
...
StringgetException(Exception e)
get Exception
StringWriter w = new StringWriter();
e.printStackTrace(new PrintWriter(w));
return w.toString();
StringgetExceptionDetails(Exception e)
Gets the details of this exception.
if (e == null)
    return "";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
e.printStackTrace(ps);
return "Exception thrown: " + e.getClass().getName() + "\n" + e.getMessage() + "\n"
        + new String(baos.toByteArray());