newFileNamed (how to handle the cancel option?)

Ned Konz ned at squeakland.org
Thu Jan 13 15:19:43 UTC 2005


On Thursday 13 January 2005 5:05 am, chenais wrote:
> Hello!!
> I have a small question. I save data into files using following:
> f := FileStream newFileNamed: aFilename.
> f ifNotNil: [
>  f nextPutAll: (myobject sixxString).
>   f close ]
>
> If 'aFilename' already exits, I get from newFileNamed a menu:
> - overwrite...
> - choose another name...
> - cancel
>
> cancel generates an error.
> My question is how can I get rid of this error?
>
> Thank you for your help.
> Patrick Chénais
>
> University of Bern
> Informatikdienste

Just handle the FileExistsException. Whatever you return from the exception 
handler block will be the return value from newFileNamed:

[ f := FileStream newFileNamed: 'fred' ]
on: FileExistsException
do: [ :ex | Transcript print: ex fileName; cr. ex return: nil ]

-- 
Ned Konz
http://bike-nomad.com/squeak/



More information about the Squeak-dev mailing list