- Inline throw
- Function:
Error.Record()
signature - See Also
- Examples from the Docs
- Extra Requirements Specific to Try Catch
- Try Expression Types
- yaml summary
error
is the name of a language keyword that throws an exception. There's a built in function named Error.Record
to construct the argument record
for you
error [
Reason = "Business Rule Violated",
Message = "Item codes must start with a letter",
Detail = "Non-conforming Item Code: 456"
]
To get a GUID to easily filter query tracing, you can Table.AddColumn()
a guid for traces using trace specific rows to specific Diagnostics.ActivityId() as text
= Diagnostics.ActivityId()
Creates the record to throw
Error.Record(
reason as text,
optional message as nullable text,
optional detail as any,
optional parameters as nullable list) as record
- what's new: New Power Query M Language Keyword: catch
- pq docs Error.Record()
- Diagnostics.Trace()
- Diagnostics.ActivityId() as text
- what's new: bengribaudo.com: Structured Error Messages
- bengribaudo.com: power-query-m-primer-part-15-error-handling
- Power Query Formal Language Specs.pdf
The Ellipsis
expression is sugar for throwing this expression
x =...
is equivalent to
x = error Error.Record("Expression.Error", "Not Implemented")
This expression
x = error Error.Record(
"FileNotFound", "File my.txt not found", "my.txt")
is equivalent to
x = error [
Reason = "FileNotFound",
Message = "File my.txt not found",
Detail = "my.txt"
]
more examples
- The catch function must be defined inline, so a reference to a function cannot be used.
each
cannot be used to define thecatch-function
- the function defintion cannot include any type constraints
= Table.AddColumn(
Source,
"Clean Standard Rate",
each
try [Standard Rate]
catch (e) =>
if e[Message] = "Invalid cell value '#REF!'." then [Special Rate] * 2
else if e[Message] = "Invalid cell value '#DIV/0!'." then [Special Rate] / 3
else 0
)
Functionally equivalent to try <e> otherwise <e>
let
x = try 1 / 0
catch () => "null"
in
x
let
x = try 1 / 0
otherwise "null"
in
x
let
x = try "A"
in
if x[HasError] then x[Error] else x[Value]
returns:
type: record
reason:
type: text
default: "Expression.Error"
notes: . |
the default value isn't mandatory, may be implementation details and change
message:
type: optional nullable text
detail:
type: optional detail as any
parameters:
type: optional nullable list