Simulating python's With statement in java

Posted by drozzy on Stack Overflow See other posts from Stack Overflow or by drozzy
Published on 2010-05-31T18:36:56Z Indexed on 2010/05/31 18:43 UTC
Read the original article Hit count: 184

Filed under:
|
|

Is there something like Python with context manager in Java?

For example say I want to do something like the following:

getItem(itemID){
   Connection c = C.getConnection();
   c.open();
   try{
    Item i = c.query(itemID);
   }catch(ALLBunchOfErrors){
      c.close();
   }

   c.close();
   return c;
}

where in python I just have:

with( C.getConnection().open() as c):
   Item i = c.query(itemID);
   return i;

© Stack Overflow or respective owner

Related posts about java

Related posts about python