1

I'm trying to setup a method to make sure all the data I need is available before proceeding to the next part of my component. So I have something like this:

public Boolean getIsDataLoaded(){
    /* loop through possible data points */
    for(Integer i : params.keySet()){
        try{
            /* Attempt to get the data for this parameter */
            String p = getParam(i); // Which then uses getValue(i)
        } catch(CustomException e){
            return false;
        }
    }
}

This loop causes each parameter to be run through the getValue() method. This method then uses a parser method from another class which will throw custom exceptions. Previously I was able to run each one of these steps using a throw/catch every step of the way.

I want to know now, can I just have the one catch I have here instead of one at each level up abstraction passing it "down the line". I was under the impression that as long as someone caught it then I wouldn't get the Salesforce error page.

4
  • Why don't you put the whole for loop into a try block? – Boris Bachovski Mar 2 '16 at 5:02
  • Tried that too, didn't work so that's why I thought I was doing something wrong to not catch it soon enough – Xtremefaith Mar 2 '16 at 5:05
  • When you say 'every step of the way', do you mean for each item in the loop, or within each layer of abstraction in the code? i.e. Are you essentially asking whether your getValue() method can just not handle the exception from the parser, then let higher up methods handle it? – Nick Cook Mar 2 '16 at 5:05
  • @NickCook this is correct, per each layer of abstraction, hoping to not include in getValue() or others – Xtremefaith Mar 2 '16 at 5:06
2

Given your getIsDataLoaded() method is just returning a yes or no, couldn't you do something like:

public Boolean getIsDataLoaded(){
    Boolean isLoaded = true;
    /* loop through possible data points */
    for(Integer i : params.keySet()){
        try{
            /* Attempt to get the data for this parameter */
            String p = getParam(i); // Which then uses getValue(i)
        } catch(CustomException e){
            isLoaded = false;
        }
    }

    return isLoaded;
}

You're essentially testing with the String p = getParam(i); and if that passes the data must be loaded. You keep a current state of whether the data is loaded in isLoaded and if all parameters pass, you return true.

Either way, it doesn't matter where the exception is thrown, you just need to ensure it's caught in some way.

For example, if you want to display detailed information about what failed, you'd want to capture the exception and (assuming this is on a VF page) you can add it to a pageMessages component.

Not sure if this is what you're after though.

5
  • I like that addition, but the CustomException is still not getting caught, so the page is displaying a salesforce override error screen – Xtremefaith Mar 2 '16 at 5:12
  • Are you sure it's the CustomException that's not being caught, and not a generic exception? you can always change the CustomException to just Exception. – Nick Cook Mar 2 '16 at 5:13
  • I changed to Exception and that did not work. I removed the throw new CustomException and the error went away... I think I just need to clean up some of my methods at this point it could be an oversite – Xtremefaith Mar 2 '16 at 5:17
  • I'd suggest the simplified version of your code is missing something then. If getParam() or any method it calls is throwing an exception, the defined try/catch block will catch it. By definition, you're throwing a CustomException so it must be catchable, especially seeing as you're receiving an error on the page. – Nick Cook Mar 2 '16 at 5:25
  • 1
    Catching generic Exceptions is... a bad code smell at best. I like to call them a pokemon catch. Gotta catch 'em all! You should know what type of exception you are looking for in almost every scenario. – Adrian Larson Mar 2 '16 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.