You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if (".xi" %chin% names(x)) stopf("x contains a column called '.xi'. Conflicts with internal use by data.table.")
55
55
for (iincols) {
56
56
.xi=x[[i]] # [[ is copy on write, otherwise checking type would be copying each column
57
-
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported as a key column type, currently.", i, typeof(.xi))
57
+
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported as a key column type, currently.", i, typeof(.xi), class="dt_unsortable_type_error")
58
58
}
59
59
if (!is.character(cols) || length(cols)<1L) internal_error("'cols' should be character at this point") # nocov
if (any(miss)) stopf("some columns are not in the data.table: %s", brackify(cols[miss]))
269
+
if (any(miss)) stopf("some columns are not in the data.table: %s", brackify(cols[miss]), class="dt_missing_column_error")
270
270
if (".xi" %chin% colnames(x)) stopf("x contains a column called '.xi'. Conflicts with internal use by data.table.")
271
271
for (iincols) {
272
272
.xi=x[[i]] # [[ is copy on write, otherwise checking type would be copying each column
273
-
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported for ordering currently.", i, typeof(.xi))
273
+
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported for ordering currently.", i, typeof(.xi), class="dt_unsortable_type_error")
274
274
}
275
275
if (!is.character(cols) || length(cols)<1L) internal_error("'cols' should be character at this point") # nocov
if ( is.character(x[[rc]]) &&!(is.character(y[[lc]]) || is.factor(y[[lc]])) ) {
17
-
stopf("When x's column ('%s') is character, the corresponding column in y ('%s') should be factor or character, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]))
17
+
stopf("When x's column ('%s') is character, the corresponding column in y ('%s') should be factor or character, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]), class="dt_join_type_mismatch_error")
stopf("When x's column ('%s') is factor, the corresponding column in y ('%s') should be character or factor, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]))
19
+
stopf("When x's column ('%s') is factor, the corresponding column in y ('%s') should be character or factor, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]), class="dt_join_type_mismatch_error")
stopf("When x's column ('%s') is integer or numeric, the corresponding column in y ('%s') can not be character or logical types, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]))
21
+
stopf("When x's column ('%s') is integer or numeric, the corresponding column in y ('%s') can not be character or logical types, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]), class="dt_join_type_mismatch_error")
Copy file name to clipboardExpand all lines: vignettes/datatable-programming.Rmd
+44Lines changed: 44 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -456,6 +456,50 @@ DT[, eval(cl)]
456
456
DT[, cl, env = list(cl = cl)]
457
457
```
458
458
459
+
## Error handling with classed conditions
460
+
461
+
Starting from version 1.17.0, `data.table` provides specific error classes for common operations, making it easier to handle errors programmatically. This is particularly useful when writing robust code or packages that use `data.table`.
462
+
463
+
### Available error classes
464
+
465
+
`data.table` now provides four specific error classes:
466
+
467
+
-`dt_missing_column_error`: When referencing columns that don't exist
468
+
-`dt_invalid_input_error`: When providing invalid input types or empty required arguments
469
+
-`dt_unsortable_type_error`: When trying to sort/key unsupported types
470
+
-`dt_join_type_mismatch_error`: When column types are incompatible in joins/set operations
DT1 <- data.table(id = 1:3, value = c("a", "b", "c"))
488
+
DT2 <- data.table(id = 1:3, value = 1:3)
489
+
490
+
tryCatch({
491
+
fintersect(DT1, DT2)
492
+
}, dt_join_type_mismatch_error = function(e) {
493
+
cat("Type mismatch in join:", conditionMessage(e), "\n")
494
+
}, error = function(e) {
495
+
cat("Other error:", conditionMessage(e), "\n")
496
+
})
497
+
```
498
+
499
+
### Backward compatibility
500
+
501
+
All error classes inherit from base R's condition system, so existing `tryCatch(..., error = ...)` code continues to work unchanged. The new classes simply provide more specific handling options when needed.
0 commit comments