Errors in Pascal
In Pascal, error handling is typically done using exceptions. Here’s an explanation of the key differences and adaptations:
Instead of returning errors, we raise exceptions in Pascal.
We define a custom exception type
ECustomError
that inherits from the built-inException
class.The
F
function now raises an exception instead of returning an error.We create global exception instances
ErrOutOfTea
andErrPower
to serve as sentinel errors.The
MakeTea
procedure raises exceptions instead of returning errors.In the
main
program, we usetry
-except
blocks to handle exceptions.To check for specific errors, we compare the caught exception with our sentinel errors or check the error message.
Pascal doesn’t have a direct equivalent to Go’s
errors.Is
function. Instead, we compare exception instances directly or check the error message content.
When you run this program, it will produce output similar to the Go version, demonstrating error handling in Pascal:
This example shows how to implement error handling in Pascal using exceptions, which is the idiomatic way to handle errors in this language.