Best way to return status flag and message from a method in Java

后端 未结 17 1242
清酒与你
清酒与你 2020-12-25 12:55

I have a deceptively simple scenario, and I want a simple solution, but it\'s not obvious which is \"most correct\" or \"most Java\".

Let\'s say I have a small authe

17条回答
  •  醉酒成梦
    2020-12-25 13:33

    I would choose the Exception option in first place.

    But, in second place, I would prefer the C-style technique:

    public boolean authenticate(Client client, final StringBuilder sb) {
        if (sb == null)
            throw new IllegalArgumentException();
        if (isOK()) {
            sb.append("info message");
            return true;
        } else {
            sb.append("error message");
            return false;
        }
    }
    

    This is not so strange and it's done in many places in the framework.

提交回复
热议问题