Power Query: try ... otherwise on list item expression

Viewed 330

Why does catching the following index-error fail:

let
    Source = "abc",
    letter_list = Text.ToList(Source),
    try_on_index_error = try letter_list{5}
in
    try_on_index_error

The try statement does not return an Error record. It keeps throwing the error as if no try was present.

In this case it works as expected, returning an Error record as a valid result of the query:

let
    Source = "abc",
    try_another_error = try Source + 1,
    Error = try_another_error[Error]
in
    Error

https://docs.microsoft.com/en-us/power-query/handlingerrors#catching-an-error-with-try-and-otherwise

https://docs.microsoft.com/en-us/powerquery-m/m-spec-consolidated-grammar#error-handling-expression

1 Answers

It is naturally hard to answer questions on the behavior of closed-source software. However, the comment by Ron Rosenfeld solved my trouble:

letter_list = List.Buffer(Text.ToList(Source))

The docs (last paragraph in chapter Evaluation) explain the unexpected behavior: Lazy evaluation is done for lists, records and let-expressions. As seen above, in case of lazy evaluation, try statement falls back to syntax checking, supposedly.

Related