The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Cleaning up external resources

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Cleaning up external resources Posted: Feb 13, 2006 4:58 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Cleaning up external resources
Feed Title: Cincom Smalltalk Blog - Smalltalk with Rants
Feed URL: http://www.cincomsmalltalk.com/rssBlog/rssBlogView.xml
Feed Description: James Robertson comments on Cincom Smalltalk, the Smalltalk development community, and IT trends and issues in general.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Cincom Smalltalk Blog - Smalltalk with Rants

Advertisement

Bob Congdon has a post up on the difficulties of cleaning up external resources in Java if exceptions get thrown while they are in use. He points to a C# feature that ensures that such resources will be finalized, the using statement:


using (TextWriter tw = File.CreateText("log.txt")) {
      tw.WriteLine("Foo");      
      // Other stuff  
} 

Apparently, the compiler expands that. In Smalltalk, we don't need a language feature for that - we have the #ensure: method. It looks like this:


stream := 'someFile' asFilename writeStream.
[stream nextPutAll: 'Foo'.
"other code here"]
     ensure: [stream ifNotNil: [stream close]].

#ensure takes a block (closure) as an argument, and will execute regardless - if there are exceptions inside the block it's sent to or not. It's a very clean way to make sure that necessary code runs, when you aren't particularly concerned with the exact nature of any exceptions that pop up.

Read: Cleaning up external resources

Topic: SEO Tricks can cost you a lot Previous Topic   Next Topic Topic: Making the quality-factor visible

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use