O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action—Loading the list

Loading the list is a bit more complicated; it is possible that the file does not exist and in this case, we just want to start with a new list.

Let's see how we can handle that:

public static function init()
{
//Read the data from the zookeeper.data file
try
{
animals = haxe.Unserializer.run(php.io.File.getContent ("zookeeper.data"));
} catch(e:Dynamic)
{
//If reading from the file doesn't work
//We can initialize a new list of animals
animals = new List<Animal>();
}
}

Ok, it is not so complicated in fact. We just had to wrap our reading with a try block. It could have been handled a bit better—we could have just had a look to see if the file existed. If it existed but the unserializing had failed, then it would ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required