Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 mean that we have a true problem with the stored data.

So, here is our complete Zoo class:

package zooKeeper;

class Zoo
{
   public static var animals : List<Animal...