function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eager-2-LearnEager-2-Learn 

Exception handling best practice question

Is it best to check for NULL or allow an exception to fired?  For example would you use this...

 

if ( somevariable == NULL ) {

   // do something

} else {

  // do something

}

 

Or would you do this...

 

try {

    somevariable = anothervariable.length();

 

} catch ( Exception e )  {

   // catch the error from the erro that occurs from the length check above

}

 

 

 

I want to say the first approach is the best because I always thought it was best prevent an error if possible and when it is not possible use Exception to catch it.  What say you?  I just want to do the best approach when it comes to coding.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jaxdmasterjaxdmaster

Always work in tried and tested manner. Sometimes salesforce is so unpredictable that you cant figure what is going on.

Apex is similar to Java. Always write SOQL queries in try/catch block. Check for null values as many as possible. Dont write too much complicated logic and break your code into multiple statements. 

All Answers

jaxdmasterjaxdmaster

Well I use exception when I can't handle errors pragmatically. Like SQL exceptions.

 

I use first approach while writing business logic and use later one when firing SOQL queries.

Eager-2-LearnEager-2-Learn

Thanks for the response jaxdmaster.  I know this is mudding up this subject post but could you answer one more question about proper coding?   I feel like I know the answers but I just want that extra feeling of being correct.

 

 

The function below is in a class (didn't include complete class for simplicity) and I included the call code to the function.

I feel that this is not the best way to write the routine and here is why:

 

     *  Starting at the call to the funciton--it does not store the return value! What say you?

     *  The exception catch does nothing and it doesn't have a return value!  What say you?

     *  If an error does occur would this be an issue since there is no return?  I thought all functions had to have a return value!  Maybe it works because it is a boolean return value but it seems like bad practice.

 

These questions may seem obvious to some but I do not feel fully confident in my assumptions and that is why I am reaching out to the community.

 

 

public boolean validRequest { get;set; }

validRequest = true;

if ( bla bla bla )

   validateIt();

}

 

 

 

private boolean validateIt {
        try {
           // do some work here

        } catch (Exception e) {
            // The real code would not have anything in this catch!

        }
        return validRequest;
    }

jaxdmasterjaxdmaster

Always work in tried and tested manner. Sometimes salesforce is so unpredictable that you cant figure what is going on.

Apex is similar to Java. Always write SOQL queries in try/catch block. Check for null values as many as possible. Dont write too much complicated logic and break your code into multiple statements. 

This was selected as the best answer