The Language Reference & Guide
- Welcome to Emojicode
- Syntax
- The Basics
- Literals
- Variables and Assignment
- Control Flow
- Classes & Value Types
- Overloading
- Operators
- Optionals
- Errors
- Inheritance and Overriding
- Protocols
- Enumerations
- Types and Namespaces
- Types as Values
- Documentation
- Generics
- Callables
- Packages
- Threads
- Safe and Unsafe Code
- Memory Management
- References
- Appendix: The Emojicode Compiler
Errors
Proper mechanisms to handle errors are an integral part of modern programming languages. Being a modern language, Emojicode provides a sophisticated but light-weight way to handle errors.
Error-Proneness
Emojicode supports error handling for any kind of method, initializer or closure.
In Emojicode errors are represented by instance of the π§ or its subclasses. For instnace, the class π§πΈβοΈ is used to represent errors that occur during input/output operations, such as when reading a file.
In case a method, initializer or closure may fail, it should be declared as error-prone. Error-proneness is indicated using the identifier π§ directly before the functionβs body.
error-type βΆ π§ type
The π§ identifier is immediately followed by the type of error that may be raised by the function. In the example below, the first and second declaration indicate that the declared class method and initializer may raise input/output errors while the last method could raise any kind of error. This type is referred to as the error type.
πβοΈ π path π‘ π§π§πΈβοΈ π π ...
ππ path π‘ π§π§πΈβοΈ π π ...
πβοΈ π€¦ββοΈ β‘οΈ π‘ π§π§ π π ...
πβοΈ π π§π§ π π ...
Raising Errors
To raise an error the π¨ statement, which works similar to β©οΈ, is used.
raise βΆ π¨ expression
The expression must evaluate to an object instance compatible to the declared error type:
πβοΈ π€¦ββοΈ β‘οΈ π‘ π§π§ π
π¨ππ§π€Too low on chargeπ€βοΈ
π
Calling Error-Prone Functions
An error-prone method, initializer or callable cannot be called without explicit handling of potential errors. If you try anyway, youβll get a compiler error. There are three options.
πΊ Not Handling Errors
As with optionals, you can use πΊ to make a call to an error-prone function and disregard the possibility of an error arising. If an error, however, is raised during execution the program will panic.
πΊπππ π€file.txtπ€βοΈ β‘οΈ file
Unless you are absolutely sure that a call will never raise an error, using πΊ is a bad idea.
πΊ Reraising Errors
πΊ can be used to reraise all arising errors. This means that if an error is returned by the called function, the calling function itself will raise the error and return immediately. Naturally, the calling function must declare an error type to which the error type of the called function is compatible.
πβοΈ π β‘οΈ π‘ π§π§ π
β©οΈπΊπ€¦ββοΈππβοΈ
π
In the above example, the π class method will return the value returned by π€¦ββοΈ if returns normally. If π€¦ββοΈ raises an error though, π will forward it to its own caller.
reraise βΆ πΊ expression
π Handling Errors
The third mechanism is a control flow statment. It allows you to specify to code blocks. While one is executed in the case of error-free execution, the other is called in the case of an error and provided with the error object.
error-check-control βΆ π [variable] expression block π
variable block
The provided expression must be an error-prone call. If no error is raised, the first block is called and the variable, if provided, will contain the returned value. If an error does occur, the second block is called an the specified variable will be set to the error. The variableβs type is the error type of the called function.
Example:
π a π€¦ββοΈππβοΈ π
π a βοΈ
π
π
error π
π π€An error occured: π§²π¬errorβοΈπ§²π€βοΈ
π
You must not provide a variable if the call does not return a value. You may omit a variable name even though the function returns a value if you do not require the return.
π π€¦ββοΈππβοΈ π π We are not interested in the return
π ...
π
π
error π
π π€An error occured: π§²π¬errorβοΈπ§²π€βοΈ
π
π πππβοΈ π π π does not return a vlue
π ...
π
π
error π
π π€An error occured: π§²π¬errorβοΈπ§²π€βοΈ
π
Error-Prone Super Initializer Calls
If you call a super initializer that might fail, your initializer must be declared error-prone with an error type to which the error type of the super initializer is compatible to. In case the super initializer raises an error, initialization is aborted and the calling initializer reraises the error.
π π« π§ π
π ...
π
π π π
π π π§π« π
π ...
π
π
π π‘ π π
ππ
π§π« π
‴οΈπβοΈ
π
π