Intermediate 25 min read

Handle Errors

By the end of this lesson, you'll be able to:

  • Reject a shipment that would push stock negative
  • Guard against receiving a shipment for a product with no existing stock record
  • Use a custom exception for a genuinely invalid inventory operation

Prerequisites: "Search Across Warehouses (SOSL)"

A custom exception for inventory errors

public class InventoryException extends Exception {}

Exactly Module 22's LibraryException pattern, reused for a different domain — a dedicated exception type lets inventory-specific failures be caught distinctly from generic Apex exceptions.

Rejecting a shipment that would go negative

public void shipOrder(Id warehouseStockId, Decimal quantityShipped) {
    Warehouse_Stock__c stock = [
        SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId
    ];

    if (stock.Quantity_On_Hand__c < quantityShipped) {
        throw new InventoryException(
            'Cannot ship ' + quantityShipped + ' units — only ' + stock.Quantity_On_Hand__c + ' available.'
        );
    }

    stock.Quantity_On_Hand__c -= quantityShipped;
    update stock;
}

This upgrades the earlier lesson's return false; into a throw with a specific, informative message — a genuine business-rule violation (shipping more than exists) now stops the operation loudly rather than just signaling failure through a return value.

Guarding against a missing stock record

public Warehouse_Stock__c getStockRecord(Id productId, String warehouseName) {
    List<Warehouse_Stock__c> matches = [
        SELECT Id, Quantity_On_Hand__c
        FROM Warehouse_Stock__c
        WHERE Product__c = :productId AND Warehouse_Name__c = :warehouseName
    ];

    if (matches.isEmpty()) {
        throw new InventoryException('No stock record exists for this product at this warehouse.');
    }

    return matches[0];
}

Rather than assuming a Warehouse_Stock__c record always exists for any product/warehouse combination, this method checks matches.isEmpty() (Module 15) first — receiving stock for a genuinely new warehouse should create a fresh record, not silently fail while looking for one that was never set up.

Exercise

Given a Warehouse_Stock__c already queried, write a guard that throws InventoryException if Quantity_On_Hand__c is less than a requested shipQuantity.

Show hint

if (stock.Quantity_On_Hand__c < shipQuantity) { throw new InventoryException(...); }

APEX

Handle Errors Quiz

1. Why does getStockRecord check matches.isEmpty() before returning a result?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

This lesson hardens InventoryService against two real misuses: shipping more stock than exists, and adjusting stock for a product/warehouse combination that was never set up.