Java Exception to String getErrorMessage(Exception ex)

Here you can find the source of getErrorMessage(Exception ex)

Description

get Error Message

License

Open Source License

Declaration

public static String getErrorMessage(Exception ex) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 jialiang.//from   w  w w .  ja  v a 2s . co m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Ben Xu, xufengbing@gmail.com - initial API and implementation
 *     jialiang, lantianjialiang@gmail.com - add copy right and fix warning
 ******************************************************************************/

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

public class Main {
    public static String getErrorMessage(Exception ex) {
        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();
        }
        return returnStr;
    }
}

Related

  1. exceptionToString(Throwable ex)
  2. exceptionToString(Throwable exception)
  3. exceptionTypeAndMsg(Exception e)
  4. exceptionWithCause(T exception, Throwable cause)
  5. getErrorMessage(Exception ex)
  6. getErrorMessage(Process process)
  7. getException(Exception e)
  8. getExceptionDetails(Exception e)
  9. getExceptionMessage(Exception e)