Skip to content

Commit 555fcc7

Browse files
added vignettes
1 parent 68cc421 commit 555fcc7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

vignettes/datatable-programming.Rmd

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,50 @@ DT[, eval(cl)]
456456
DT[, cl, env = list(cl = cl)]
457457
```
458458

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
471+
472+
### Usage examples
473+
474+
```{r error_handling, error=TRUE}
475+
DT <- data.table(a = 1:3, b = 4:6)
476+
477+
# Handle missing column errors specifically
478+
tryCatch({
479+
setkey(DT, "nonexistent_col")
480+
}, dt_missing_column_error = function(e) {
481+
cat("Missing column detected:", conditionMessage(e), "\n")
482+
}, error = function(e) {
483+
cat("Other error:", conditionMessage(e), "\n")
484+
})
485+
486+
# Handle type mismatches in operations
487+
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.
502+
459503
```{r cleanup, echo=FALSE}
460504
options(.opts)
461505
registerS3method("print", "data.frame", base::print.data.frame)

0 commit comments

Comments
 (0)