I had to run some tests to convince myself that this had the same effect as the old code... For posterity:

```
# Original version
old <- function(file, func) {  
  png(file)
  tryCatch(
    func(),
    finally=dev.off()
  )
  99
}

# Yihui's version
new <- function(file, func) {  
  png(file)
  local({
    dv <- dev.cur()
    on.exit(dev.off(dv))
    func()
  })
  99
}

# Yihui's version without local()
new2 <- function(file, func) {  
  png(file)
  dv <- dev.cur()
  on.exit(dev.off(dv))
  func()
  99
}


# Case 1: error in writing file with png()
old('/foo', function() plot(1:3, 1:3))
new('/foo', function() plot(1:3, 1:3))
new2('/foo', function() plot(1:3, 1:3))

# Case 2: error in func()
old('./foo.png', function() plot(1:3, 1:2))
new('./foo.png', function() plot(1:3, 1:2))
new2('./foo.png', function() plot(1:3, 1:2))
```

Behavior is the same in all cases (got error, no return value). I did notice that the `traceback()` stack after old, new, and new2 is different for each, which makes sense. Is there any reason we need to keep the `local()`?