Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 10 / 68

Error handling (try-catch)

This lesson covers error handling with Julia's try-catch, with the aim of keeping a program running even when something unexpected goes wrong.

try / catch / end catches an error so the whole program does not stop. Think of it as insurance: deciding in advance what to do instead, should something fail.

In the sample code, a divide() function raises an error with error() on division by zero, and try ... catch e ... end catches it. Note that catch e gives you the error information in the variable e.

A common early stumble is how flexible the syntax is. It works without naming a variable after catch, but you need one to get at the details of the error.

In professional work, try-catch is used around anything that can fail, such as reading data or a numerical routine that may not converge.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)