Talk Rust 10: Error Handling

PrimerPy
5 min readApr 9, 2024
The Burning of the Library of Alexandria, 1876. Private Collection. Photo: Fine Art Source: Getty Images

In most modern languages, there are a systematic way to handle errors, e.g. in Python, we usually use try...except...(else...final...) for such purposes. However this type of error handling is problematic.

Different Types of Errors

While coding, there are potentially two types of errors: exceptions and failure.

  • Exceptions are tied to the programs themselves, e.g. IndexError: list index out of range error in Python or ArrayIndexOutOfBoundsException in Java, they are usually caused by the bugs in the codes
  • Failures are usually not directly related to the programs, e.g. failed to connect to a database or failed to establish a TCP connection. These are not caused by the programming bugs and can be resolved e.g. with more repetitive try

In Python, these two types of errors are both referred as Exception making it a bit complex to differentiate and debug.

In Rust, the errors are divided into recoverable and unrecoverable errors:

  • Unrecoverable errors panic! are always symptoms of bugs e.g. index out of bound, which we should terminate the program immediately
  • Recoverable errors Result<T,E>are like file not found, connection failed to establish errors. Rust would report such errors to users and retry the…

--

--

PrimerPy
PrimerPy

Written by PrimerPy

www.primerpy.com | A Primer on Python. I'm a veteran Data and Web developer based out of NYC and DC area. I blog mainly on Python and other tech related topics.

No responses yet