📄 tij311.htm
字号:
}
<font color=#0000ff>private</font> <font color=#0000ff>int</font> hasField(String id) {
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < fields.length; i++)
<font color=#0000ff>if</font>(id.equals(fields[i][0]))
<font color=#0000ff>return</font> i;
<font color=#0000ff>return</font> -1;
}
<font color=#0000ff>private</font> <font color=#0000ff>int</font>
getFieldNumber(String id) <font color=#0000ff>throws</font> NoSuchFieldException {
<font color=#0000ff>int</font> fieldNum = hasField(id);
<font color=#0000ff>if</font>(fieldNum == -1)
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> NoSuchFieldException();
<font color=#0000ff>return</font> fieldNum;
}
<font color=#0000ff>private</font> <font color=#0000ff>int</font> makeField(String id) {
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < fields.length; i++)
<font color=#0000ff>if</font>(fields[i][0] == <font color=#0000ff>null</font>) {
fields[i][0] = id;
<font color=#0000ff>return</font> i;
}
<font color=#009900>// No empty fields. Add one:</font>
Object[][]tmp = <font color=#0000ff>new</font> Object[fields.length + 1][2];
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < fields.length; i++)
tmp[i] = fields[i];
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = fields.length; i < tmp.length; i++)
tmp[i] = <font color=#0000ff>new</font> Object[] { <font color=#0000ff>null</font>, <font color=#0000ff>null</font> };
fields = tmp;
<font color=#009900>// Reursive call with expanded fields:</font>
<font color=#0000ff>return</font> makeField(id);
}
<font color=#0000ff>public</font> Object
getField(String id) <font color=#0000ff>throws</font> NoSuchFieldException {
<font color=#0000ff>return</font> fields[getFieldNumber(id)][1];
}
<font color=#0000ff>public</font> Object setField(String id, Object value)
<font color=#0000ff>throws</font> DynamicFieldsException {
<font color=#0000ff>if</font>(value == <font color=#0000ff>null</font>) {
<font color=#009900>// Most exceptions don't have a "cause" constructor.</font>
<font color=#009900>// In these cases you must use initCause(),</font>
<font color=#009900>// available in all Throwable subclasses.</font>
DynamicFieldsException dfe =
<font color=#0000ff>new</font> DynamicFieldsException();
dfe.initCause(<font color=#0000ff>new</font> NullPointerException());
<font color=#0000ff>throw</font> dfe;
}
<font color=#0000ff>int</font> fieldNumber = hasField(id);
<font color=#0000ff>if</font>(fieldNumber == -1)
fieldNumber = makeField(id);
Object result = <font color=#0000ff>null</font>;
<font color=#0000ff>try</font> {
result = getField(id); <font color=#009900>// Get old value</font>
} <font color=#0000ff>catch</font>(NoSuchFieldException e) {
<font color=#009900>// Use constructor that takes "cause":</font>
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
}
fields[fieldNumber][1] = value;
<font color=#0000ff>return</font> result;
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
DynamicFields df = <font color=#0000ff>new</font> DynamicFields(3);
System.out.println(df);
<font color=#0000ff>try</font> {
df.setField(<font color=#004488>"d"</font>, <font color=#004488>"A value for d"</font>);
df.setField(<font color=#004488>"number"</font>, <font color=#0000ff>new</font> Integer(47));
df.setField(<font color=#004488>"number2"</font>, <font color=#0000ff>new</font> Integer(48));
System.out.println(df);
df.setField(<font color=#004488>"d"</font>, <font color=#004488>"A new value for d"</font>);
df.setField(<font color=#004488>"number3"</font>, <font color=#0000ff>new</font> Integer(11));
System.out.println(df);
System.out.println(df.getField(<font color=#004488>"d"</font>));
Object field = df.getField(<font color=#004488>"a3"</font>); <font color=#009900>// Exception</font>
} <font color=#0000ff>catch</font>(NoSuchFieldException e) {
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
} <font color=#0000ff>catch</font>(DynamicFieldsException e) {
<font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
}
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"null: null"</font>,
<font color=#004488>"null: null"</font>,
<font color=#004488>"null: null"</font>,
<font color=#004488>""</font>,
<font color=#004488>"d: A value for d"</font>,
<font color=#004488>"number: 47"</font>,
<font color=#004488>"number2: 48"</font>,
<font color=#004488>""</font>,
<font color=#004488>"d: A new value for d"</font>,
<font color=#004488>"number: 47"</font>,
<font color=#004488>"number2: 48"</font>,
<font color=#004488>"number3: 11"</font>,
<font color=#004488>""</font>,
<font color=#004488>"A value for d"</font>,
<font color=#004488>"Exception in thread \"</font>main\<font color=#004488>" "</font> +
<font color=#004488>"java.lang.RuntimeException: "</font> +
<font color=#004488>"java.lang.NoSuchFieldException"</font>,
<font color=#004488>"\tat DynamicFields.main(DynamicFields.java:98)"</font>,
<font color=#004488>"Caused by: java.lang.NoSuchFieldException"</font>,
<font color=#004488>"\tat DynamicFields.getFieldNumber("</font> +
<font color=#004488>"DynamicFields.java:37)"</font>,
<font color=#004488>"\tat DynamicFields.getField(DynamicFields.java:58)"</font>,
<font color=#004488>"\tat DynamicFields.main(DynamicFields.java:96)"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Each <b>DynamicFields</b> object contains an array of <b>Object-Object</b> pairs. The first object is the field identifier (a <b>String</b>), and the second is the field value, which can be any type except an unwrapped primitive. When you create the object, you make an educated guess about how many fields you need. When you call <b>setField( )</b>, it either finds the existing field by that name or creates a new one, and puts in your value. If it runs out of space, it adds new space by creating an array of length one longer and copying the old elements in. If you try to put in a <b>null</b> value, then it throws a <b>DynamicFieldsException</b> by creating one and using <b>initCause( )</b> to insert a <b>NullPointerException</b> as the cause. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0511" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>As a return value,<b> setField( )</b> also fetches out the old value at that field location using <b>getField( )</b>, which could throw a <b>NoSuchFieldException</b>. If the client programmer calls <b>getField( )</b>, then they are responsible for handling <b>NoSuchFieldException</b>, but if this exception is thrown inside <b>setField( )</b>, it’s a programming error, so the <b>NoSuchFieldException</b> is converted to a <b>RuntimeException</b> using the constructor that takes a <i>cause</i> argument. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0512" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545370"></a><a name="_Toc24775708"></a><a name="Heading8840"></a>Standard
Java exceptions</h2>
<p>The Java class <b>Throwable</b> describes anything that can be thrown as an exception. There are two general types of <b>Throwable</b> objects (“types of” = “inherited from”). <a name="Index819"></a><b>Error</b> represents compile-time and system errors that you don’t worry about catching (except in special cases). <a name="Index820"></a><b>Exception</b> is the basic type that can be thrown from any of the standard Java library class methods and from your methods and run-time accidents. So the Java programmer’s base type of interest is usually <b>Exception</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1557" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The best way to get an overview of the exceptions is to browse the HTML Java documentation that you can download from <i>java.sun.com.</i> It’s worth doing this once just to get a feel for the various exceptions, but you’ll soon see that there isn’t anything special between one exception and the next except for the name. Also, the number of exceptions in Java keeps expanding; basically, it’s pointless to print them in a book. Any new library you get from a third-party vendor will probably have its own exceptions as well. The important thing to understand is the concept and what you should do with the exceptions. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap10_1558" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The basic idea is that the name of the exception represents the problem that occurred, and the exception name is intended to be relatively self-explanatory. The exceptions are not all defined in <b>java.lang</b>; some are created to support other libraries such as <b>util</b>, <b>net,</b> and <b>io</b>, which you can see from their full class names or what they are inherited from. For example, all I/O exceptions are inherited from <b>java.io.IOException</b>. <font size="-2"><a href="mailto:TIJ3@Min
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -