Catch When Things Go Wrong

You might be wondering why you would want to be writing risky instructions in the first place. What makes an instruction risky? Remember in Chapter 2, “How Software Works,” we wrote some fake code to open a Microsoft Word document? See Listing 7.30 for a refresher. That code is actually quite risky.

Listing 7.30 Open a Microsoft Word Document (Example from Chapter 2)

function openWordFile(filePath) {  var wordWindow = openWordWindow();  var fileContents = loadFile(filePath);  displayFile(wordWindow, fileContents);}

The risky part of that code is loadFile(filePath). What if the file path isn’t valid? What if the file was deleted? What if the file path points to an external hard drive that ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.