Error Handling and Tasks

    This section is going to walk through three data structures that help you handle errors in a couple different ways.

    • Result

    Now some of you probably want to jump right to tasks, but trust me that going in order will help here! You can think of these three data structures as a progression that slowly address crazier and crazier situations. So if you jump in at the end, it will be a lot to figure out all at once.

    There are two popular language features that consistently cause unexpected problems. If you have used Java or C or JavaScript or Python or Ruby, you have almost certainly had your code crash because of values or surprise exceptions from someone else’s code.

    Any time you think you have a String you just might have a null instead. Should you check? Did the person giving you the value check? Maybe it will be fine? Maybe it will crash your servers? I guess we will find out later!

    The inventor, Tony Hoare, has this to say about it:

    Exceptions

    Joel Spolsky outlined the issues with exceptions pretty nicely . Essentially, code that looks fine may actually crash at runtime. Surprise!

    The point of Result is to make the possibility of failure clear and make sure it is handled appropriately.

    The point of Task is pretty much the same, but it also works when we have code that runs asynchronously. Your error handling mechanism shouldn’t totally fall apart just because you make an HTTP request!