
Apex Try-Catch Blocks That Actually Help You Debug
June 19, 2025
We’ve all seen it — an Apex class with a try-catch block that does… nothing.
```apex try { // business logic } catch (Exception e) { // 🙈 quietly fail }
That kind of error handling makes troubleshooting a nightmare. If something breaks, you get no logs, no visibility, and no clue why.
Instead, here’s how we structure try-catch blocks to help us debug efficiently:
Log the error using System.debug(), or better yet, store it in a custom Error Log object with e.getMessage() and e.getStackTraceString() Surface user-friendly messages only when appropriate — don’t expose internals in UI errors Avoid overbroad exception types — catching everything hides root causes And when possible, wrap risky logic in helper methods to isolate failures. If you’re logging email failures the same way you handle DML errors, it’s time to refactor.
Solid error handling turns surprises into signals — and lets you fix issues before your users even notice.