scope file.open

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Jan 24 05:52:26 PST 2007


"nobody" <not at possible.de> wrote in message 
news:ep7395$o92$1 at digitaldaemon.com...

> The question is, how scope know it's a failure to open a file, before the 
> file.open statement
> is executed.
> That is i didn't understand.

What the scope(failure) statement does is "registers" the following piece of 
code to be executed whenever the current scope (brace block) is left.  So 
you can put it anywhere:

scope(failure) writefln("Datei kann nicht geoeffnet werden ");
File file = new File;
file.open("testdatei.txt",FileMode.In);
while(!file.eof()) writefln(file.readLine());
file.close();

Or:

File file = new File;
scope(failure) writefln("Datei kann nicht geoeffnet werden ");
file.open("testdatei.txt",FileMode.In);
while(!file.eof()) writefln(file.readLine());
file.close();

Or even:

File file = new File;
file.open("testdatei.txt",FileMode.In);
while(!file.eof()) writefln(file.readLine());
file.close();
scope(failure) writefln("Datei kann nicht geoeffnet werden ");

It doesn't matter where the scope(failure) appears.  It just means "if an 
exception passes out of this block, run this code." 




More information about the Digitalmars-d-learn mailing list