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 orArrayIndexOutOfBoundsException
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…