jq · lesson 31 of 32 · Errors
try, catch & ?
Some filters fail on some inputs — tonumber on "two", for instance. try f catch g recovers with a fallback, and f? simply drops the failure.
Examples
Example 1A fallback for bad input
Document
["1", "two", "3"]
Query
.[] | try tonumber catch "n/a"
try tonumberRuntonumber; if it raises an error…catch "n/a"…produce this instead. Insidecatch,.is the error message.
Result
1 "n/a" 3
Example 2Silently skip failures
Document
["1", "two", "3"]
Query
[.[] | tonumber?]tonumber?A trailing?istrywithout acatch: values that fail just produce no output.
Result
[1,3]
Good to know
error("message")raises your own error:try error("boom") catch "caught"gives"caught".