Book Image

Getting Started with Julia

By : Ivo Balbaert
Book Image

Getting Started with Julia

By: Ivo Balbaert

Overview of this book

Table of Contents (19 chapters)
Getting Started with Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The Rationale for Julia
Index

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 for arr[0] causes a program to stop with ERROR: BoundsError()

  • Calling sqrt() on a negative value, for example, sqrt(-3) causes ERROR: DomainError: sqrt will only return a complex result if called with a complex argument, try sqrt(complex(x)); The sqrt(complex(-3)) function gives the correct result 0.0 + 1.7320508075688772im

  • A syntax error in Julia code will usually result in LoadError

Similar to these there are 18 predefined exceptions that Julia can generate (refer to http://docs.julialang.org/en/latest/manual/control-flow/#man-exception-handling). They are all derived from a base type, Exception.

How can you signal an error condition yourself...