what is best way to leave a groovy script prematurely (except system.exit(0))

Just use System.exit(0).

try {
    // code
} catch(Exception e) {
    logger("exception on something occurred "+e,e)
    System.exit(0)
}

You could use the exit status code to indicate what line you had problems with.

A zero value would indicate that everything was OK, and a positive value would be the line number. You could then let your groovy script take the start line as an input parameter.


This is a naive implementation with just a silly exception if a line is empty.

file = new File(args[0])
startLine = args[1].toInteger()

file.withReader { reader ->
    reader.eachLine { line, count ->
        try {
            if (count >= startLine) {
                if (line.length() == 0) throw new Exception("error")
                println count + ': ' + line
            }
        } catch (Exception ignore) {
            System.exit(count)
        }
    }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)