Consume a TryFunction return value when the caller handles the failure
A [TryFunction] catches errors only when the caller uses its Boolean return value. An assignment or conditional makes the invocation a try-method call; a bare call is treated as an ordinary procedure call and exposes errors as usual.
- microsoftlayer
- error-handlingdomain
- 2examples
- 4pull requests
Rule on GitHub · microsoft layer · error-handling · BC 13.. · tier official · derived
Description
A [TryFunction] catches errors only when the caller uses its Boolean return value. An assignment or conditional makes the invocation a try-method call; a bare call is treated as an ordinary procedure call and exposes errors as usual. The attribute alone does not make every invocation non-throwing, so code that handles the failure of a bare call never sees that failure: the error leaves the procedure before the handling runs.
Best Practice
When the caller must continue, log, count, or translate the failure, consume the result directly: assign it to a Boolean or use the call in an if condition. Handle false immediately while the last-error state still describes that failure.
When the error should simply propagate, a bare call is correct and needs no change; see microsoft/knowledge/error-handling/bare-tryfunction-call-propagates-errors.md.
See sample: ignored-tryfunction-return-disables-try-semantics.good.al.
Anti Pattern
Calling a [TryFunction] procedure as a standalone statement while the surrounding code expects the failure to be caught. Detect it from code, not from the call shape alone: the bare call is followed by a GetLastErrorText, GetLastErrorCode, or GetLastErrorObject check, a failure branch, failure logging, or a false/failure result; or it sits in a loop that is meant to continue past failed items. None of that handling sees the call’s error, because the error stops the procedure first.
A bare call with no such handling is intended propagation, not this anti-pattern.
See sample: ignored-tryfunction-return-disables-try-semantics.bad.al.
See also
microsoft/knowledge/performance/use-tryfunction-for-error-catching-not-rollback.md owns transaction rollback expectations after a try method has actually caught an error.
References
- Handling errors using try methods: “If a try method call uses the return value in an
OK:=statement or a conditional statement such asif-then, errors are caught.”
Examples
- Good: ignored-tryfunction-return-disables-try-semantics.good.al
- Bad: ignored-tryfunction-return-disables-try-semantics.bad.al
Names
- Debugger.GetLastErrorText() Method (names GetLastErrorText)
- System.GetLastErrorCode() Method (names GetLastErrorCode)
- System.GetLastErrorObject() Method (names GetLastErrorObject)
Changed by
- #211 knowledge(error-handling): a bare [TryFunction] call propagates its error, 2026-11-17
- #174 Make BCQuality documentation easier for partners to get started, 2026-09-09
- #108 Complete AL review knowledge readiness, 2026-07-15
- #99 Add lifecycle error and privacy knowledge, 2026-07-14
From microsoft/BCQuality at aaaf6c1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).