Swan Lake Beta3 is here.
import ballerina/io;

// `n` must not be `0`.
function divide(int m, int n) returns int {

    if n == 0 {
        // Panic if `n` is `0`.
        panic error("division by 0");

    }
    return m/n;
}

public function main() {
    int x = divide(1, 0);

    // Since `divide(1, 0)` panics, the program will
    // terminate and the following code will not be
    // executed.
    io:println(x);

}

Panics

Ballerina distinguishes normal errors from abnormal errors. Normal errors are handled by returning error values. Abnormal errors are handled using the panic statement. Abnormal errors should typically result in immediate program termination. e.g., A programming bug or out of memory. A panic has an associated error value.

import ballerina/io;
function divide(int m, int n) returns int {

n must not be 0.

    if n == 0 {
        panic error("division by 0");

Panic if n is 0.

    }
    return m/n;
}
public function main() {
    int x = divide(1, 0);
    io:println(x);

Since divide(1, 0) panics, the program will terminate and the following code will not be executed.

}
bal run panics.bal
error: division by 0
        at panics:divide(panics.bal:8)
           panics:main(panics.bal:15)
import ballerina/io;

// `n` must not be `0`.
function divide(int m, int n) returns int {

    if n == 0 {
        // Panic if `n` is `0`.
        panic error("division by 0");

    }
    return m/n;
}

public function main() {
    int x = divide(1, 0);

    // Since `divide(1, 0)` panics, the program will
    // terminate and the following code will not be
    // executed.
    io:println(x);

}

Panics

Ballerina distinguishes normal errors from abnormal errors. Normal errors are handled by returning error values. Abnormal errors are handled using the panic statement. Abnormal errors should typically result in immediate program termination. e.g., A programming bug or out of memory. A panic has an associated error value.

import ballerina/io;
function divide(int m, int n) returns int {

n must not be 0.

    if n == 0 {
        panic error("division by 0");

Panic if n is 0.

    }
    return m/n;
}
public function main() {
    int x = divide(1, 0);
    io:println(x);

Since divide(1, 0) panics, the program will terminate and the following code will not be executed.

}
bal run panics.bal
error: division by 0
        at panics:divide(panics.bal:8)
           panics:main(panics.bal:15)
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand