Exception handling
When executing a program, abnormal conditions can occur, which force the Julia runtime to throw an exception or error, show the exception message and the line where it occurred, and then exit. For example (follow along with the code in chapter 4\errors.jl
):
- Using the wrong index for an array, for example,
arr = [1,2,3]
and then asking forarr[0]
causes a program to stop withERROR: BoundsError()
- Calling
sqrt()
on a negative value, for example,sqrt(-3)
causesERROR: DomainError: sqrt will only return a complex result if called with a complex argument, try sqrt(complex(x))
; Thesqrt(complex(-3))
function gives the correct result0.0 + 1.7320508075688772im
- A syntax error in Julia code will usually result in
LoadError
Similar to
Get Getting Started with Julia 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.