3

I wanna stop ballerina program in the middle of some logic. How can I stop a running program in ballerina using code? I'm looking for something equivalent to System.exit(0) in java.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
  • 1
    I think there is no need for a System.exit sort of function in Ballerina. This is because, if you started your process via a main function, then getting that function to complete will result in the program exiting. If there are services running, then it is a bad idea to have a System.exit in service code. – Afkham Azeez May 22 '18 at 05:14

3 Answers3

5

I believe you are writing a program with a main function. Unfortunately its not available with Ballerina, you can request that feature by creating an issue in Github repository [1].

As @werner has suggested throwing an error would be a workaround as following code.

import ballerina.io;

function main(string[] args) {
    int count = 0;
    while (true) {
        io:println(count);
        if (count == 5) {
            error err = {message:"Stop the program"};
            throw err;
        }
        count = count + 1;
    }
}

[1] https://github.com/ballerina-lang/ballerina/issues

Tharik Kanaka
  • 2,490
  • 6
  • 31
  • 54
  • Issue created => No possibility to exit program #5311 https://github.com/ballerina-lang/ballerina/issues/5311 – Werner Mar 17 '18 at 20:08
1

You could throw a runtime exception.

Werner
  • 281
  • 1
  • 12
1

I think there is no need for a System.exit sort of function in Ballerina. This is because, if you started your process via a main function, then getting that function to complete will result in the program exiting. If there are services running, then it is a bad idea to have a System.exit in service code.

Afkham Azeez
  • 154
  • 5
  • Let's think that we created an API, there should be a way to send response back to the client and stop further processing (ex: payload validation). As you said end of the program ends execution, but that increase lines code, maintaining status, excessing usage of status checks etc.. Simply having some statement expressing "no further processing needed" for this request, improve UX when we writing integrations – Milinda Perera Jul 17 '19 at 06:07