package com.test.collection;
public class test3 {
public static void main(String[] args)
{
/*
* jdk1.7之前的try-catch形式
*/
try{
Resource res = new Resource();
ResourceOther resOther = new ResourceOther();
res.doSome();
resOther.doSome();
} catch (Exception ex) {
ex.printStackTrace();
}finally{
//释放资源
}
System.out.println("--------------------------------------");
/*
* jdk1.7之后新添加的try-with-resource形式
* 在try-with-resource中也可以有catch与finally块。
// 只是catch与finally块是在处理完try-with-resource后才会执行。
* 可以使用autoCloseable接口自动关闭资源
*/
try(Resource ress=new Resource();ResourceOther resss=new ResourceOther())
{
ress.doSome();
resss.doSome();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
class Resource implements AutoCloseable {
void doSome() {
System.out.println("do something");
}
@Override
public void close() throws Exception {
System.out.println("resource closed");
}
}
class ResourceOther implements AutoCloseable {
void doSome() {
System.out.println("do something other");
}
@Override
public void close() throws Exception {
System.out.println("other resource closed");
}
}
public class test3 {
public static void main(String[] args)
{
/*
* jdk1.7之前的try-catch形式
*/
try{
Resource res = new Resource();
ResourceOther resOther = new ResourceOther();
res.doSome();
resOther.doSome();
} catch (Exception ex) {
ex.printStackTrace();
}finally{
//释放资源
}
System.out.println("--------------------------------------");
/*
* jdk1.7之后新添加的try-with-resource形式
* 在try-with-resource中也可以有catch与finally块。
// 只是catch与finally块是在处理完try-with-resource后才会执行。
* 可以使用autoCloseable接口自动关闭资源
*/
try(Resource ress=new Resource();ResourceOther resss=new ResourceOther())
{
ress.doSome();
resss.doSome();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
class Resource implements AutoCloseable {
void doSome() {
System.out.println("do something");
}
@Override
public void close() throws Exception {
System.out.println("resource closed");
}
}
class ResourceOther implements AutoCloseable {
void doSome() {
System.out.println("do something other");
}
@Override
public void close() throws Exception {
System.out.println("other resource closed");
}
}