关于方法头异常的抛出
As with Java methods that are part of the supplied classes, you declare that your method may throw an exception with an exception specificationin the method header.
class MyAnimation
{
. . .
public Image loadImage(String s) throws IOException
{
. . .
}
}
If a method might throw more than one checked exception type, you must list all exception classes in the header. Separate them by a comma as in the following example:
class MyAnimation
{
. . .
public Image loadImage(String s) throws EOFException, MalformedURLException
{
. . .
}
}
子类方法不能比父类抛出更多异常
If you override a method from a superclass, the checked exceptions that the subclass method declares cannot be more general than those of the superclass method. (It is ok to throw more specific exceptions, or not to throw any exceptions in the subclass method.)
关于assert
The sole purpose of the expression part is to produce a message string. The AssertionError object does not store the actual expression value, so you can’t query it later. As the JDK documentation states with paternalistic charm, doing so “would encourage programmers to attempt to recover from assertion failure, which defeats the purpose of the facility.”
To assert that x is nonnegative, you can simply use the statement
assert x >= 0;
Or you can pass the actual value of x into the AssertionError object, so that it gets displayed later.
assert x >= 0 : x;
By default, assertions are disabled. You enable them by running the program with the -enableassertions or -ea option:
java -enableassertions MyApp
You can also disable assertions in certain classes and packages with the -disableassertions or -da option:
java -ea:... -da:MyClass MyApp
Some classes are not loaded by a class loader but directly by the virtual machine. You can use these switches to selectively enable or disable assertions in those classes. However, the -ea and -da switches that enable or disable all assertions do not apply to the “system classes” without class loaders. Use the -enablesystemassertions/-esa switch to enable assertions in system classes.
何时使用assert
* Assertion failures are intended to be fatal, unrecoverable errors.
* Assertion checks are turned on only during development and testing. (This is some times jokingly described as “wearing a life jacket when you are close to shore, and throwing it overboard once you are in the middle of the ocean.”)
logging
Logger.global.info("File->Open menu item selected");
By default, the record is printed like this:
May 10, 2004 10:12:15 PM LoggingImageViewer fileOpen
INFO: File->Open menu item selected
禁用日志:
Logger.global.setLevel(Level.OFF);
多于一个模板类型的表示方法
A generic class can have more than one type variable.
public class Pair<T, U> { . . . }
加上类型限定的表示
<T extends BoundingType>
expresses that T should be a subtype of the bounding type.
Both T and the bounding type
can be either a class or an interface.
A type variable or wildcard can have multiple bounds. For example:
T extends Comparable & Serializable