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

来源:互联网 发布:移动网络改成电信 编辑:程序博客网 时间:2024/05/17 09:36

关于方法头异常的抛出

 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 




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 广告机格式化了怎么办 proe约束冲突了怎么办 中午考试想睡觉怎么办 喝了红茶睡不着怎么办 考试前状态不好怎么办 考前紧张睡不着觉怎么办 通宵失眠第二天怎么办 考试前睡不着觉怎么办 一到晚上睡不着怎么办 明天考试听力差怎么办 天天晚上睡不着觉怎么办 因紧张睡不着觉怎么办 房卡找不到了怎么办 马代不会英文怎么办 剩下的蒸米饭怎么办 临时牌照违法了怎么办 打12345不管用怎么办 省政府改变了中央文件怎么办 应聘时学历不够怎么办 做导游学历不够怎么办 市长热线打不通怎么办 12315网站不受理怎么办 法律文书生效前转后财产怎么办 打12345投诉没用怎么办 单位医保停了怎么办 iphonex开不了机怎么办 会计证三年没检怎么办 银行工作人员态度不好怎么办 生殖保健服务证怎么办 关机后自动开机怎么办 大连卖房户口怎么办 在警务室被辅警骂了怎么办 上海微信怎么办居住证 公共自行车卡消磁怎么办 港澳通行证到期了怎么办 徐海路大桥对面怎么办 没有买公务机票怎么办 电脑黑屏怎么办重启也没有用 航空公司原因航班取消怎么办 政府采购2次废标怎么办 车卖给别人车牌怎么办