Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Errors
Ballerina does not have exceptions. Instead functions report invalid states by returning error values. Each error value has,
- 1.Message, a human-readable
string
value describing the error.
- 2.Cause, which is an
error
value if this error was caused due to another error, which needs to be propagated, otherwise nil.
- 3.Detail, a mapping value consisting of additional information about the error.
- 4.Stack trace, a snapshot of the state of the execution stack when the error value was created.
Error values are immutable.
You can create a new error value using an error constructor. As the first argument to the error constructor, it expects the message string. As the second argument, you can optionally pass in an error?
value for cause. Subsequent named arguments, if specified, will be used to create the detail mapping. The stack trace is provided by the runtime.
import ballerina/io;
type Person record {
string name;
int age;
};
function validatePeople(Person[] people) returns error? {
if people.length() == 0 {
// Create an error value specifying only the error message.
return error("Expected a non-empty array");
}
foreach Person p in people {
io:println("Validating ", p.name);
error? err = validatePerson(p);
if err is error {
// Create a new error value with the validation error as the cause
// and the `Person` value for which validation failed in the detail mapping.
return error("Validation failed for a person", err, person = p);
}
}
}
function validatePerson(Person person) returns error? {
int age = person.age;
if age < 0 {
// If validation fails for age, create a new error value specifying
// an error message and the age value for which validation failed.
return error("Age cannot be negative", age = age);
}
}
public function main() {
Person[] people = [
{name: "Alice", age: 25},
{name: "Bob", age: -1},
{name: "Charlie", age: 30}
];
// Note how the `Person` value after the value for which validation fails is
// not processed.
error? err = validatePeople(people);
if err is error {
printError(err);
}
}
// Helper function to print internals of error value.
function printError(error err) {
io:println("Message: ", err.message());
io:println("Detail: ", err.detail());
io:println("Stack trace: ", err.stackTrace());
error? cause = err.cause();
if cause is error {
io:println("Cause:");
printError(cause);
}
}
$ bal run error_reporting.balValidating AliceValidating BobMessage: Validation failed for a personDetail: {"person":{"name":"Bob","age":-1}}Stack trace: [callableName: validatePeople fileName: error_reporting.bal lineNumber: 20,callableName: main fileName: error_reporting.bal lineNumber: 42]Cause:Message: Age cannot be negativeDetail: {"age":-1}Stack trace: [callableName: validatePerson fileName: error_reporting.bal lineNumber: 30,callableName: validatePeople fileName: error_reporting.bal lineNumber: 16,callableName: main fileName: error_reporting.bal lineNumber: 42]
Related links
PreviousUnions
NextAnydata type