try catch总结

来源:互联网 发布:windows无法格式化cf 编辑:程序博客网 时间:2024/06/08 11:53

1、格式一 :

                     try{

                          .............................

                          ............................

                     }catch(){
                           ........................

                           ...................

                     }catch()

                            .......................

                            .........................

                       }finally{

                             .......................

                             ........................

                     }


主要知道:(1)try对异常进行集中的处理    {   }   不能省略,即使是一个语句

                  (2)catch对于抓异常进行捕鱼的抓法,先是小网,最后才是大网,密的网

                  (3)finally是无论如何都要执行的块


2、格式二:(这个才是我写该博客的目的

                  

                     try

                     (

                      )

                     {

                          .............................

                          ............................

                     }catch(){
                           ........................

                           ...................

                     }catch()

                            .......................

                            .........................

                       }finally{

                             .......................

                             ........................

                     }


这是在java 7新增加的内容,Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块(Automatic Resource Management) ,自动资源管理。”


理由如下:


  一般我们会编写如下代码来释放资源:

 

private static void BufferStreamCopy(File source, File target) {      //是一个文件拷贝的函数    InputStream fis = null;    OutputStream fos = null;    //声明字节流在外边好方便一会再finally块里进行释放    try {        fis = new FileInputStream(source);        fos = new FileOutputStream(target);         byte[] buf = new byte[1024];   //声明一个数组用来暂时存储输入流读入的字节!!         int i;        while ((i = fis.read(buf)) != -1) {   //将源文件的字节读到 “ buf  ” 数组中             fos.write(buf, 0, i);  //   从数组中都字节到输出流中,然后输出流,另一头接着target文件        }    }    catch (Exception e) {        e.printStackTrace();    } finally {        close(fis);        close(fos);    }} private static void close(Closeable closable) {    if (closable != null) {        try {            closable.close();        } catch (IOException e) {            e.printStackTrace();        }    }}


使用 try-with-resources 语句来简化代码如下:


private static void BufferStreamCopy(File source, File target) {    try (InputStream fis = new FileInputStream(source);        OutputStream fos = new FileOutputStream(target)){         byte[] buffer = new byte[1024];         int i;        while ((i = fis.read(buffer)) != -1) {            fos.write(buffer, 0, i);        }    }    catch (Exception e) {        e.printStackTrace();    }}
代码清晰很多吧?在这个例子中,数据流会在 try 执行完毕后自动被关闭,

前提是,

这些可关闭的资源(InputStream            OutputStream)   必须   实现 java.lang.AutoCloseable 接口(接口中有一个方法)


    ------------------抽象的字节流,字符流都已经实现了该接口


下面是api里面的close()方法

close

void close()           throws IOException
关闭此流并释放与此流关联的所有系统资源。如果已经关闭该流,则调用此方法无效。

抛出:
IOException - 如果发生 I/O 错误

        

原创粉丝点击