Writing a Compiler - Error Handling compiler language Concepts Total vs Partial Functions Backtraces Recoverable and Unrecoverable Errors

Errors usually fall into two categories, recoverable and unrecoverable.

Error Handling Methods Signaling Errors by Returning a Special Value

Languages like Rust and Go handle recoverable errors by returning special values like Result>< and error.

Exceptions

A function unable to return valid values may throw an exception that encodes the cause of the error. This is the prefered way of handling errors in languages like C++ and D.

Exception Shortcomings Unrecoverable Errors

Most languages provide a mechanism to signal an error and immediately terminate the program. In Rust this is achieved by using the panic! macro. The program then will print a failure message, clean up the stack and exit. In C and C++ you can use the exit function. Rust also has an exit function, however panic! is prefered.

Design Ignore Errors

Functions are free to ignore errors, and they will bubble up all the way to the main function. An unhandled error will terminate the program with a non-zero exit code.

Terminate the Program Throw Errors Handling Errors

Errors are handled by the >! operator or the equivalent catch function. The error handler must return the same type as the previous statement in the stream, or rethrow the error.