All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reference.language.errors.html Maven / Gradle / Ivy




    
    Errors
    
    

    
    

    
    

    
    
    
    

    


Error handling

Error handling is critical to perception of quality. Even is your application code is correct, it may generate errors. These errors may emanate from hardware (disk, network, memory), from middleware (credentials, service down), or from your application itself if your code meets undesirable situations and raises corresponding user defined errors.

Prompto implements error handling using a try/fail mechanism. Here is an example:

In the above example, we try to perform an illegal operation (divide by zero). Without precaution, this error would simply force the application to abruptly exit. To avoid this, we protect the potential error by wrapping our code inside a try/fail block.

A try/fail block has the following structure:

  • switch on error doing:
     list of protected statements
  • when type:
     list of statements
  • when in [ type1, type2, ... ]:
     list of statements
  • otherwise or when any
     list of statements
  • always:
     list of statements
where error is a variable name which will be populated with the error object, and can be used in the corresponding handler section.
The block must comprise at least one handler, either a when, a when in, a when any or an always clause.
There can be any number of when or when in clauses.
If present, the when any or otherwise clause must come after all when and when any clauses, and before the always clause.
If present, the always clause must be the last one.

    Examples of correct sequences:
  • switch on error doing:
        list of statements
    when in [NOT_MUTABLE, NOT_STORABLE]:
        list of statements
  • switch on error doing:
        list of statements
    always:
        list of statements
  • switch on error doing:
        list of statements
    when NULL_REFERENCE:
        list of statements
    otherwise:
        list of statements
    
  • switch on error doing:
        list of statements
    when any:
        list of statements
    always:
        list of statements
    

The execution is as follows:

  • Prompto tries to execute the list of protected statements
  • if an error occurs:
    • Prompto looks for the first when clause that accepts the error type
      • if such a clause exists, Prompto executes the correponding list of statements
      • otherwise if a when any clause exists, Prompto executes the correponding list of statements
  • whether or not an error occurs:
    • if there is an always clause, Prompto executes the correponding list of statements

Built-in errors

Prompto is focused on business logic, so it does not provide a wide variety of error types. Instead it lets the underlying runtime provide details on those errors.

All built-in errors are defined in the built-in Error enumerated category.

The list of built-in errors is:

  • READ_WRITE: a read/write error can occur when reading or writing from a resource: file, url, database and so forth
  • NULL_REFERENCE: a null reference occurs when trying to access a member attribute or method from a null object
  • INDEX_OUT_OF_RANGE: this error occurs when trying to access a list element < 1 or > to the number of elements in the list
  • DIVIDE_BY_ZERO: this error occurs when trying to divide a number by 0
  • NOT_MUTABLE: this error occurs when trying to set a member attribute on a non mutable object
  • NOT_STORABLE: this error occurs when trying to stor an object of a category not marked as storable

User defined errors

There are situations not covered by built-in errors. For example, if your program reads text from a file, and expects certain content in that text but cannot find it, you might want to raise a specific error.

Prompto lets you define errors, which extend the built-in Error enumeration of errors.

Here is an example:

define BadContentError as enumerated Error with symbols:
    MISSING_PRODUCT with "Expecting 'product' in file!" as text
    MISSING_PRICE with "Could not find 'price' for product!" as text

User defined errors simply accept a text attribute, which can be displayed or logged.

Once user errors are defined, they can be raised using the raise keyword:





© 2015 - 2025 Weber Informatics LLC | Privacy Policy