Sometimes, coding defensively won't be the solution. Maybe a key part of your program requires reading a file on the network or accessing a database. If the resource can't be accessed due to a temporary network failure, there's really not much you can do in the absence of the data.
If you identify parts of your code where you think the execution can go off the rails due to conditions that are out of your control (that is, exceptional conditions—hence the name exception), you can use Julia's try...catch
statements. This is exactly what it sounds like—you instruct the compiler to try a piece of code and if, as a result of a problem, an exception is thrown, to catch it. The fact that an exception is caught implies that it won't propagate throughout the whole application.
Let's see it in action:
julia> try
getattr(dom.root, "href")
catch
println("The $(tag(dom.root)) tag doesn't have a 'href' attribute.")
end
The HTML tag doesn...