Emojicode Documentation 1.0 beta 1
Guides Language Reference Package Index

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-Proness

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-proness 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.

πŸ‡ 🐫 🚧 πŸ‡
  πŸ’­ ...
πŸ‰

πŸ‡ 🐟 πŸ‡
  πŸ†• πŸ†’ 🚧🐫 πŸ‡
    πŸ’­ ...
  πŸ‰
πŸ‰

πŸ‡ 🐑 🐟 πŸ‡
  πŸ†•πŸ‘… 🚧🐫 πŸ‡
    β€΄οΈπŸ†’β—οΈ
  πŸ‰
πŸ‰
← Previous Next Up: β€œInheritance and Overriding” β†’
Something not quite right? Improve this page