简体   繁体   中英

Cost of Java Exception Handling and Performance

Following code will throw exception "String index out of range: -1". From Java performance perspective, is it better to check if index > -1 or just let the code throw the exception? And let framework deal with Logging it?

String abc = "somedata";
if (abc != null) {
    int index = abc.lastIndexOf(',');
    abc = abc.substring(0, index);
}

= EDIT = I thought I will summarize the discussions/views.

There has been a small debate amongst some of my developer friends about it. I respect both views, described below.

** Developer's thought behind not worrying about "String index out of range: -1" **

A legitimate user will submit correct data and will get the right result. A hacker/scripter/not-legitimate use of the application posting invalid data may see a broken page in UI but the least that I care about. I let the system throw the exception, let the framework catch and log it. Benefit of letting it get logged tells me that there are scripters abusing my system.

** My view about it **

After knowing that there are scripters/hackers trying to abuse my system, then what? IMO, about any important reputed system over the web, mostly all will be regularly getting daily huge scripted requests from hacker systems. No valiation or proof is required for that. Identifying and blocking them is a job of WAF system, not of my code to log thousands of exceptions everyday to tell there is a hacker.

I agree with mvd's answer that instead of throwing/catching/logging. We got to decide what we want to do when we get invalid data posted, either it causes a non-happy business flow, or it is completely flawed request that I don't care about. If I care then do alternate business flow scenario, and if I don't care then ignore and log a warning no need to throw/catch/throw/catch/log etc.

Again, it is debatable and decisions may be subject to case-by-case scenarios.

Thanks everyone for your responses.

I cannot speak to the performance costs, but I will say that it is preferable NOT to use exceptions to drive application logic.

Exceptions are just that, exceptions. If you can program and explain your reasoning in code, then that is better than using an exception.

public String substringOnComma(String value) {
    String abc = "";

    if (value != null) {
        if (containsComma(value) {
            int index = value.lastIndexOf(',');
            abc = value.substring(0, index);
        }
    }

    return abc;
}


public boolean containsComma(String value) {
    boolean comma = false;

    if (value.lastIndexOf(',') != -1) {
        comma = true;
    }

    return comma;
}

It is always preferable to check the value instead of catching the exception, both for performance and maintainability reasons. Checking if the value is -1 is a single instruction for modern CPUs, throwing and catching an exception may be hundreds depending on the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM