<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>


    Java provides programmers with capability to elegantly handle runtime errors.

    1. Throwable has two subclasses: Exception and Error.

    RuntimeException, Error and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meansing that the compiler forces the programmer to check and deal with them.

    2. Handling Expections
    (1) Declearing Exceptions
    use throws keyword
    public void myMethod() throws Exception1, Exception2, ...
    (2) Throwing Exceptions
    IllegaArgumentException ex = new IllegaArgumentException("Wrong Argument");
    or
    throw new IllegaArgumentException("Wrong Argument");
    (3) Catching Exceptions
    try {
    statements;
    }
    catch (Exception1 exVar1) {
    handler1;
    }
    catch (Exception2 exVar2) {
    handler2;
    }
    ...
    Note that Exceptioni can't be subclass of Exceptionj (i
    When handling exceptions, java.lang.Throwable.getMessage()/toString()/printStackTrace() are useful.
    3. Rethrowing
    try
    {
    access the database
    }
    catch (SQLException e)
    {
    throw new ServletException("database error: " + e.getMessage());
    }
    4. the finally clause
    Graphics g = image.getGraphics();
    try
    {
    // 1
    code that might throw exceptions
    // 2
    }
    catch (IOException e)
    {
    // 3
    show error dialog
    // 4
    }
    finally
    {
    // 5
    g.dispose();
    }
    the code in the finally block is executed under all circumstances, regardless of whether an exception occures in the try block is caught.
    5. Don't use a try-catch block to deal with simple, expected situations.
    6. creating custon exception classes
    7. Assertions
    Assertions can be used to ensure program correctness and avoid logic errors.
    assert assertion [: detailMessage];
    detailMessage can a primitive-type or an Object value.
    When an assertion statement is executed, Java evaluates the assertion. If it is false, an AssertionError will be thrown
    By default, assertions are disabled at runtime. To enable them, use
    java -ea AssertionDemo
    Assertions can be selectively enabled or disabled at the class level or the package level.
    java -ea:package1 -da:Class1 AssertionDemo
    8. Don't use assertions for argument checking in public methods.Instead, exception handling is needed.
    Use assertions to reaffirm assumption to increase your confidence in the programme's correctness.
    if (number < 1) {
    ...
    }
    else if (number = 1) {
    ...
    }
    else assert false : number;
    switch (month) {
    case 1: ...; break;
    case 2: ...; break;
    ...
    case 12: ...; break;
    default: assert false : "Invalid monthL " + month
    }

    posts - 403, comments - 310, trackbacks - 0, articles - 7
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    2.13 Java notes - Exceptions and Assertions

    Posted on 2007-04-22 20:23 ZelluX 閱讀(177) 評論(0)  編輯  收藏 所屬分類: OOP
    2007-02-13 22:20:55
    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 免费国产成人高清在线观看麻豆| 久久综合亚洲色HEZYO国产| 日韩亚洲人成在线综合| 亚洲人成人无码网www电影首页| 四虎成人精品永久免费AV| 久久亚洲AV成人无码国产最大| 亚洲色婷婷一区二区三区| 国产精品色拉拉免费看| 色哟哟国产精品免费观看| 亚洲码在线中文在线观看| 国产成人免费片在线观看| 久久香蕉国产线看免费| 日本亚洲欧美色视频在线播放| 亚洲成a人片在线观看无码| 免费观看理论片毛片| 嫩草影院在线播放www免费观看| 亚洲字幕AV一区二区三区四区| 丝袜熟女国偷自产中文字幕亚洲| **毛片免费观看久久精品| jizz在线免费观看| 亚洲国产日韩精品| 亚洲Av无码专区国产乱码DVD| 国产嫩草影院精品免费网址| 久久久久久夜精品精品免费啦| 猫咪免费人成网站在线观看入口 | 最近新韩国日本免费观看| 美女视频黄a视频全免费网站色| 亚洲综合激情视频| 亚洲国产美女精品久久久久∴| 日本不卡在线观看免费v| 1000部拍拍拍18勿入免费视频下载| 一本久久免费视频| 亚洲欧美国产精品专区久久| 麻豆亚洲AV永久无码精品久久| 亚洲综合亚洲综合网成人| 精品久久久久久久免费人妻 | 亚洲爆乳无码专区| 亚洲免费在线观看| 全黄a免费一级毛片人人爱| 最新中文字幕免费视频| 四虎在线成人免费网站|