January 2020
Intermediate to advanced
532 pages
13h 31m
English
There is actually a performance overhead to use a try-catch block. In particular, if the application is doing something in a tight loop, it would be a bad idea to catch exceptions inside the loop. To understand the impact, let's try a simple example.
Consider the following code that simply calculates the sum of the square root of every number in an array:
function sum_of_sqrt1(xs) total = zero(eltype(xs)) for i in eachindex(xs) total += sqrt(xs[i]) end return totalend
Knowing that sqrt may throw DomainError for negative numbers, our first attempt may be to catch such exceptions inside the loop:
function sum_of_sqrt2(xs) total = zero(eltype(xs)) for i in eachindex(xs) try total += ...
Read now
Unlock full access