Condition Handling with Classed Conditions
data.table-condition-classes.Rddata.table provides specific condition classes for common operations, making it easier to handle conditions programmatically. This is particularly useful when writing robust code or packages that use data.table. Relying on the exact text of condition messages is fragile (it is not uncommon to change the wording slightly, or for the user's session not to be in English); prefer using the signal class where possible.
Details
Available Condition Classes
data.table provides the following specific condition classes:
Error Classes:
dt_missing_column_error: When referencing columns that don't existdt_invalid_input_error: When providing invalid input types or empty required argumentsdt_unsortable_type_error: When trying to sort/key unsupported typesdt_join_type_mismatch_error: When column types are incompatible in joins/set operationsdt_invalid_let_error: When using assignment operators incorrectly
Warning Classes:
dt_missing_fun_aggregate_warning: When aggregation function is missing in operations that require it
Examples
# Handle missing column errors specifically
DT <- data.table(a = 1:3, b = 4:6)
tryCatch({
setkey(DT, nonexistent_col)
}, dt_missing_column_error = function(e) {
cat("Missing column detected:", conditionMessage(e), "\n")
}, error = function(e) {
cat("Other error:", conditionMessage(e), "\n")
})
#> Missing column detected: some columns are not in the data.table: [nonexistent_col]