ApexSensei

Drills Classes & objects

Recover from division by zero

try holds code that might fail; a matching catch runs instead if it does, and later code still runs.

Your task

Wrap calls to a calculator in try/catch so dividing by zero is caught and later code still runs.

class RatioCalculator {
    public Decimal divide(Decimal numerator, Decimal denominator) {
        return (numerator / denominator).setScale(2);
    }
}

RatioCalculator calculator = new RatioCalculator();

Requirements

  1. catch (MathException should be present
  2. getTypeName() should be present
  3. Normal: 4.00 should be built from runtime values
  4. Normal: 4.00 should be printed
  5. Divide by zero: System.MathException should be printed
  6. After catch: 3.00 should be printed
  7. The three lines should be printed in order

Sign in to open this drill