-2

How can exceptions be handled in Apex without using try/catch( / finally)?

closed as off-topic by Raul, Ratan Paul, Mohith Shrivastava, Oleksandr Berehovskyi, Martin Lezer Feb 19 '18 at 7:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Raul, Ratan Paul, Mohith Shrivastava, Oleksandr Berehovskyi, Martin Lezer
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I'm not sure if there is any other way. It would be helpful if you could explain what you are trying to achieve. – d_k Feb 18 '18 at 5:24
6

There are two ways to handle exceptions in Apex Code: reactive or preemptive. In reactive model of exception handling, we react to an exception, by using try-catch. In the preemptive model of exception handling, we anticipate exceptions and avoid creating situations where the exception can occur.

For example, here's the same code written in reactive handling, and preemptive handling:


// When we have an exception, we will react to it.
public static Decimal divideTwoNumbers(Decimal a, Decimal b) {
  Decimal result;
  try {
    result = a / b;
  } catch(MathException e) {
    showDivideByZeroError();
  } catch(NullPointerException e) {
    showMissingParameterError();
  }
  return result;
}

// When we run into a situation that may cause an exception,
// we simply report the error without generating the exception.
public static Decimal divideTwoNumbers(Decimal a, Decimal b) {
  Decimal result;
  if(a == null || b == null) {
    showMissingParameterError();
  } else if(b == 0) {
    showDivideByZeroError();
  } else {
    result = a / b;
  }
  return result;
}

(Note: these code snippets are illustrative, and does not address the actual bit about displaying errors, etc).

The reactive model is what we're taught at first, because it's easy. Simply put try-catch around potential problems, and the framework will handle them for you. It's also easy to abuse and can lead to hard-to-debug errors later on, especially as a beginner. Simple careless try-catch blocks can result in incorrect behavior that may go unnoticed for significant periods of time, weeks, months, or even years.

Keep in mind that both of these models have potential drawbacks. The reactive model has a huge time penalty for handling errors when they occur, while the preemptive model has a modest time penalty when there are no errors.

The time penalty is pretty significant for try-catch though; if you expect that there may ever be more than 2% of processing (e.g. in a loop of 50 items, more than 1 error), then preemptive is likely superior. Otherwise, try-catch is probably acceptable, but keep in mind that try-catch will almost always end up being more code than simply writing guard statements (if statements that verify the operation will complete).

There is no direct alternative to using try-catch, but preemptive error checking can often lead to cleaner, clearer code that performs faster when dealing with a large set of potential errors.

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