【異常-舉例6:finally】

來源:互聯網
上載者:User

標籤:exce   print   class   tac   des   string   stat   mail   except   

finally 的特點    被 finally 控制的語句體一定會執行    特殊情況:在執行到 finally 之前 jvm 退出了(比如 System.exit(0))finally 的作用    用於釋放資源,在 IO 流操作和資料庫操作中會見到final,finally 和 finalize 的區別:final:最終的意思,可以修飾類,成員變數,成員方法    修飾類,類不能被繼承    修飾變數,變數是常量    修飾方法,方法不能被重寫finally:是異常處理的一部分,用於釋放資源。    一般來說,代碼肯定會執行,特殊情況:在執行到 finally 之前 jvm 退出了finalize:是 Object 類的一個方法,用於記憶體回收如果 catch 裡面有return 語句,請問 finally 的代碼還會執行嗎?如果會,請問是在 return前還是 return 後?會執行。前。準確的說,應該是在中間。try...catch...finally 的格式變形    A:try...catch...finally     B:try...catch     C:try...catch...catch...    D:try...catch...catch...finally     E:try...finally 這種做法的目前是為了釋放資源。
package com.test;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * @author shusheng * @description * @Email [email protected] * @date 2018/10/10 16:10 */public class FinallyDemo {    /**     *finally:被 finally 控制的語句體一定會執行     *注意:如果在執行到 finally 之前 jvm 退出了,就不能執行了。     *     *A:格式     *try...catch...finally...     *B:用於釋放資源,在 IO 流操作和資料庫操作中會見到     */    public static void main(String[] args) {        String s = "2014-11-20";        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date d = null;        try {            d = sdf.parse(s);        } catch (ParseException e) {            e.printStackTrace();            //System.exit(0);    如果有這條語句就不執行 finally 語句        } finally {            System.out.println("這裡的代碼是可以執行的");        }        System.out.println(d);    }}

 

【異常-舉例6:finally】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.