/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Jan 5, 2005 */ package ml.core.exceptions; /** * \package ml.core.exceptions * * This is a collection of common exceptions for use in the * program. * * This package is part of the core program. */ /** * Standard exception class for Moonlight|3D with the ability to output the * information very verbosely. * * @author gregor */ public class Exception extends java.lang.Exception { /** * version number needed because exceptions must be serializable */ final static long serialVersionUID = 1; /** * Create a new exception carrying a plain-text error message * * @param message plain text message for this exception */ public Exception(String message) { super(message); } /** * Create a new exception carrying a plain text error message and a nested * exception explaining the true cause for these events further. * * @param message plain text message for this exception * @param cause nested exception */ public Exception(String message, Throwable cause) { super(message, cause); } /** * Return the method in which the exception was thrown * * @return full class and method name of the location of the cause */ public String getLocation() { return getStackTrace()[0].getClassName() + "." + getStackTrace()[0].getMethodName(); } /** * Return the method in which the exception occured * * @param e the exception from which the information is drawn * @return full class and method name of the location of the cause */ protected String getLocation(java.lang.Throwable e) { return e.getStackTrace()[0].getClassName() + "." + e.getStackTrace()[0].getMethodName(); } /** * Recursive helper function used by getFullText to assemble the full message for this * exception * * @param e exception to generate partial message vor * @param level indentation level * @return partial message */ protected String getRecursiveString(java.lang.Throwable e, int level, boolean includeStackTrace) { String message; String indent = ""; for (int i = 0; i < level * 4; i++) { indent += " "; } message = indent + "Exception type: " + e.getClass().getName() + "\n"; if (e.getMessage() == null) { message += indent + "Error: [MESSAGE MISSING] \n"; } else { message += indent + "Error: " + e.getMessage() + "\n"; } message += indent + "In: " + getLocation(e) + "\n"; if (e.getCause() != null) { java.lang.Throwable nested = e.getCause(); message += indent + " " + "Nested Exception:\n"; message += getRecursiveString(nested, level + 1, includeStackTrace); } else if (includeStackTrace) { message += indent + "Stack backtrace:\n"; StackTraceElement[] stackTrace = e.getStackTrace(); for (int i = 0; i < stackTrace.length; i++) { message += indent + stackTrace[i].toString() + "\n"; } } return message; } /** * Return the full multi-line text for the exception containing the type and message * for this and all embedded exceptions as well as the stack backtrace for the innermost * exception * * @return string containing the full message */ public String getFullText() { return getRecursiveString(this, 0, true); } /** * Return a multi-line text for the exception containing the type and message * for this and all embedded exceptions. * * @return string containing a message with all the reasons of all nested exceptions */ public String getReasons() { return getRecursiveString(this, 0, false); } }