error.eh

Error handling. More...

use "error.eh"

Constants

const ERR_DIV_BY_ZERO = 109
const ERR_ILL_ARG = 105
const ERR_ILL_STATE = 106
const ERR_INTERRUPT = 110
const ERR_IO = 102
const ERR_MEDIA = 111
const ERR_NEG_ALEN = 104
const ERR_NULL = 101
const ERR_RANGE = 103
const ERR_SECURITY = 107
const ERR_SYSTEM = 100
const ERR_TYPE_MISMATCH = 108
const FAIL = 1
const SUCCESS = 0

Functions

def error(code: Int = FAIL, msg: String = null);
def Error.code(): Int;
def Error.msg(): String;
def Error.tostr(): String;

Description

Errors are thrown in exceptional situations, when program cannot proceed normally. Error object contains full trace of function calls to find out where error happened. For example this program:
// example.e
use "io"

def div(a: Int, b: Int): Int = a / b

def main(args: [String]) {
  println(div(1, 0))
}
produces the following output
Division by zero
@div(example.e:4)
@main(example.e:7)
Here you can find, what function sequence in what source caused the error (to make sources appear in trace of function calls you need to compile with -g option).

If you want to not pass error but handle it in your code, use try/catch block.

def main(args: [String]) {
  try {
    println(div(1, 0))
  } catch (var err) {
    println("Oops")
  }
}
In the last example, an err variable contains Error value. You can work with this value using functions from this header. Finally, you may throw your own errors using error function. Error code defines type of the error. Error codes 100..199 are reserved for system error types.

Constant details

const SUCCESS = 0
Indicates that no error occured. This convenient constant may be returned by main() if application ends normally. You cannot create an Error with zero error code.

const FAIL = 1
Indicates that program ends with error. This convenient constant may be returned by main() if application ends abnormally.

const ERR_SYSTEM = 100
Indicates that serious error occured in Alchemy OS. System error means that process cannot proceed normally due to system failure. For example, there is not enough memory.

const ERR_NULL = 101
Indicates that operation failed because its argument is null. Null value usually means that you forgot to assign value to a variable or structure field. Also, elements of new arrays are unset, so they return null. Some functions may return null intentionally.

const ERR_IO = 102
Indicates that operation failed because of input/output error. Errors of this kind may be thrown by all functions that work with files and network.

const ERR_RANGE = 103
Indicates that an application tries to request or assign value that is out of given range. For example, this error is thrown when program tries to access array element at index less than zero or greater than array length.

const ERR_NEG_ALEN = 104
Indicates that an application tries to create an array with negative size.

const ERR_ILL_ARG = 105
Indicates that function has received illegal or inappropriate argument.

const ERR_ILL_STATE = 106
Indicates that function has been called at an illegal or inappropriate time. In other words, application is not in appropriate state for the requested operation.

const ERR_SECURITY = 107
Indicates security violation.

const ERR_TYPE_MISMATCH = 108
Indicates that application has tried to operate on a value of the wrong type.

const ERR_DIV_BY_ZERO = 109
Indicates that application has tried to divide by zero. This error is thrown only by integer arithmetics. Floating point division successfully returns NaN.

const ERR_INTERRUPT = 110
Indicates that process was interrupted.

const ERR_MEDIA = 111
Indicates that media playback failed.

Function details

def Error.code(): Int;
Returns error code of this error.

def Error.msg(): String;
Returns detail message of this error.

def Error.tostr(): String;
Converts this error to a string containing error message and trace of function calls.

def error(code: Int = FAIL, msg: String = null);
Raises error with given error code and detail message.