Java 流異常的標準處理

來源:互聯網
上載者:User
package namespace;import java.io.*;public class IOTest {public static void main(String arg[]){FileWriter writer=null;//先裝引用定義在try 外部,這樣可以在不現的代碼塊裡調用 try{writer = new FileWriter("F:\\java.txt");//對引發異常的代碼進行try writer.write("abc");}catch(IOException ex){System.out.println(ex.toString());}finally//不要忘了對資源進行釋放 {try//close 也兩樣會引發異常,同樣也要try {if(writer!=null)       //在關閉資源的進修要判斷些資源是否存在,如果不存在,會發生異常,因為在第一句new 的時候可能就會異常,這樣再執行finally裡面的就是出現錯誤writer.close();}catch(IOException ex){System.out.println(ex.toString());}}}}/* * IO  異常處理的標準寫法 * 1. 將引用定義有外面,這樣可以全部使用,在try  catch   finally 不現的代碼塊裡面都能使用 * 2. 在釋放資源的同時,也會引發異常,同時也要捕獲 * 3. 對資源進行釋放,利用 finally 進行釋放,釋放前要對資源進行判斷,看是否存在,如果不存在,同樣也會引發異常 * 2011/10/30  14:59:50 *  *  * */

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.