[Core Java. Volume I. Fundamentals, 8th Edition]-11

关于方法头异常的抛出

 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 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值