jeudi 10 décembre 2020

java do while missing return statement

I have a method, it hints that the return statement is missing, but I don’t think the statement return -1 will be executed. Can anybody tell me why?

private int loop() {
    int retryTimes = 2;
    do {
        try {
            // simulate real business
            int value = new Random().nextInt(3);
            if (value % 3 != 0) {
                throw new RuntimeException();
            }
            return value;
        } catch (Exception e) {
            if (retryTimes <= 0) {
                throw e;
            }
        }
    } while (retryTimes-- > 0);
    // if below line not exists; prompt error: missing return statement
    return -1;
}

Aucun commentaire:

Enregistrer un commentaire