package cologne.eck.tools;
/**
* This class should help to find minor bugs.
* If printDescription is called, the program
* does not stop, but prints a description of
* the problem to stderr.
*
* call like:
* new UnexpectedValueException("newFile", "File", "is null").printDescription();
*/
@SuppressWarnings("serial")
public class UnexpectedValueException extends Exception {
private String valueType = "";
private String valueName = "";
private String message = "";
private static int stackTraceLines = 5;
/**
* An exception for an unexpected value, that should not stop
* the program.
*
* @param _valueType the type of the unexpected value as String
* @param _valueName the name of the value used in the method
* @param _message an optional message about the cause (for example: is null, to large...)
*/
public UnexpectedValueException(String _valueName, String _valueType, String _message) {
if (_valueType == null) {
this.valueType = "(no specification)";
} else {
this.valueType = _valueType;
}
if (_valueName == null) {
this.valueName = "(no specification)";
} else {
this.valueName = _valueName;
}
if (_message == null) {
this.message = "";
} else {
this.message = _message;
}
}
/**
* Prints a description of the exception and
* the first lines of the StackTrace to stderr.
*/
public final void printDescription(){
System.err.println("Unexpected value "
+ this.valueName + " of type "
+ this.valueType + ": "
+ this.message);
// print the first lines of the StackTrace:
printStackTraceLines();
}
/**
* Prints first lines (default = 5) of
* StackTrace.
*
* @param s <code>PrintStream</code> to use for output
*/
public void printStackTraceLines() {
synchronized (System.err) {
System.err.println(this);
StackTraceElement[] trace = super.getStackTrace();
int len = stackTraceLines;
if (trace.length < stackTraceLines){
len = trace.length;
}
for (int i=0; i < len; i++){
System.err.println("\tat " + trace[i]);
}
}
}
/**
* @return the stackTraceLines
*/
public static int getStackTraceLines() {
return stackTraceLines;
}
/**
* @param stackTraceLines the stackTraceLines to set
*/
public static void setStackTraceLines(int _stackTraceLines) {
stackTraceLines = _stackTraceLines;
}
}