<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    2.13 Java notes - Exceptions and Assertions

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


    網站導航:
     
    主站蜘蛛池模板: 在线看片人成视频免费无遮挡| 亚洲av午夜精品无码专区| 久久电影网午夜鲁丝片免费| 99久久成人国产精品免费| 久久亚洲AV成人无码国产最大| 亚洲黄色免费网站| 亚洲人成网77777色在线播放| 在线视频免费观看www动漫| 88xx成人永久免费观看| 青青操在线免费观看| 立即播放免费毛片一级| 亚洲中文无码mv| 亚洲六月丁香六月婷婷色伊人| 亚洲熟妇无码八AV在线播放| 免费萌白酱国产一区二区| 大学生一级特黄的免费大片视频| 2022久久国产精品免费热麻豆| 成全视频高清免费观看电视剧| 九九九国产精品成人免费视频| 美国毛片亚洲社区在线观看| 天堂亚洲国产中文在线| 亚洲成人网在线观看| 日木av无码专区亚洲av毛片| 欧洲亚洲国产清在高| 亚洲精品乱码久久久久久久久久久久| 亚洲AV永久无码精品一区二区国产 | 久久99精品免费视频| 免费播放在线日本感人片| a免费毛片在线播放| 一级毛片在线播放免费| 免费无遮挡无遮羞在线看 | 国产精品久久久久久久久久免费| 久久精品免费一区二区| 亚洲免费中文字幕| 91成人免费观看网站| 免费在线看v网址| 亚洲免费福利在线视频| 在线观看免费人成视频| 久久WWW色情成人免费观看| 成人性生交视频免费观看| 在线观看免费毛片|