My ek9 command exited 3 - was that the compiler or my program?
← Error Handling and Exceptions · Ref: Q1437
Look at stderr, because the number alone cannot tell you. When you run 'ek9 myprogram.ek9' (or ./myprogram.ek9), there are two stages, and each can end the run with a status:
1. THE ek9 COMMAND compiles your source. If that fails, ek9 exits with ITS OWN code and your program never runs: 3 when a file cannot be read, 8 when compilation fails, 9 or 10 when the program arguments do not fit, and so on (the full table is in 'ek9 -h' and on the command line page).
2. YOUR PROGRAM runs, and the ek9 command passes your program's exit status through unchanged. That is 0, a returned Integer, an uncaught Exception's code, or 1 for a Panic (see Q1431).
The two share one set of numbers. The program below exits 3 because the input has no rows. 3 is also the ek9 command's 'file access error' code, but here the compiler did nothing wrong.
HOW TO TELL THEM APART
- The stderr text says which one it was. An uncaught Exception or Panic from your program prints 'Exception in thread "main" ...' on stderr, and this program prints 'Exception in thread "main" Exception: Input has no rows: Exit Code: 3'. A compile failure prints EK9 error lines instead.
- When the ek9 command ITSELF fails, its first stderr line is 'ek9: <reason> (exit N)', for example 'ek9: compilation failed (exit 8)'. Your program's exit never carries that line, so a non-zero status without it is your program's own.
- Compile separately with 'ek9 -C myprogram.ek9' before you run anything. Its non-zero exit is always the compiler's.
- Choose your own program's codes and document them. Q1432 shows how to exit with one.
See Q1431 for every way a program sets its exit status. See Q888 for program return values.
Example
defines module qa.errorhandling.cliversusprogram defines function rowCount() -> rows as List of String <- rtn as Integer: length rows defines program ImportRows() stdout <- Stdout() rows <- List() of String count <- rowCount(rows) stdout.println(`Rows found: ${count}`) if count == 0 throw Exception("Input has no rows", 3) stdout.println("Import complete")
Other ways to ask this
- How do I tell an EK9 compile failure from my program's own exit code?
- What is the difference between the ek9 CLI exit codes and my program's exit codes?
- Why does 'ek9 myprogram.ek9' return my program's exit code?
- What does the 'ek9: ... (exit N)' line on stderr mean?
Coming from another language?
Java: javac and java are separate commands, so a compile failure and a program exit code never meet. Go: 'go run' prints 'exit status N' for the program's code, while a build failure exits 1. EK9: one command compiles and runs, so the stderr text (and the 'ek9: ... (exit N)' marker) is what tells the stages apart.
Keywords: CLI, shell script, CI, program exit code, wrapper, exit code, compile failed, ek9 command, stderr