Download this file

UnexpectedValueException.java    97 lines (85 with data), 2.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
}
}