Java Exception to String exceptionToString(final Throwable ex)

Here you can find the source of exceptionToString(final Throwable ex)

Description

exception To String

License

Apache License

Declaration

public static String exceptionToString(final Throwable ex) 

Method Source Code

//package com.java2s;
/*/*  ww w . j a va 2  s.  c  om*/
 // Licensed to DynamoBI Corporation (DynamoBI) under one
 // or more contributor license agreements.  See the NOTICE file
 // distributed with this work for additional information
 // regarding copyright ownership.  DynamoBI licenses this file
 // to you under the Apache License, Version 2.0 (the
 // "License"); you may not use this file except in compliance
 // with the License.  You may obtain a copy of the License at

 //   http://www.apache.org/licenses/LICENSE-2.0

 // Unless required by applicable law or agreed to in writing,
 // software distributed under the License is distributed on an
 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
 */

public class Main {
    public static String exceptionToString(final Throwable ex) {
        return exceptionToString(ex, "; ");
    }

    /**
     * Converts any Throwable and its causes to a String.
     *
     * @param ex Throwable to be converted
     * @param sep String the stack line separator
     *
     * @return ex as a String
     */

    public static String exceptionToString(final Throwable ex, String sep) {
        if (sep == null) {
            sep = "; ";
        }

        String result = null;
        if (ex != null) {
            Throwable t = ex;
            StringBuilder sb = new StringBuilder();
            while (t != null) {
                sb.append(t.getClass().getName());
                sb.append(":  ");
                sb.append(t.getLocalizedMessage());
                t = t.getCause();
                if (t != null) {
                    sb.append(sep);
                }
            }
            result = sb.toString();
        }
        return result;
    }
}

Related

  1. exception_details(Exception e)
  2. exception_details(Exception e, String message)
  3. exceptionFormat(E exception)
  4. exceptionStackToString(Throwable x)
  5. exceptionToString(final Exception e)
  6. exceptionToString(final Throwable throwable)
  7. exceptionToString(Throwable e)
  8. exceptionToString(Throwable e)
  9. exceptionToString(Throwable ex)