|
| 1 | +\name{data.table-condition-classes} |
| 2 | +\alias{data.table-condition-classes} |
| 3 | +\title{Condition Handling with Classed Conditions} |
| 4 | +\description{ |
| 5 | +\code{data.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 \code{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. |
| 6 | +} |
| 7 | +\details{ |
| 8 | +\subsection{Available error Classes}{ |
| 9 | +\code{data.table} provides four specific error classes: |
| 10 | +
|
| 11 | +\itemize{ |
| 12 | + \item \code{dt_missing_column_error}: When referencing columns that don't exist |
| 13 | + \item \code{dt_invalid_input_error}: When providing invalid input types or empty required arguments |
| 14 | + \item \code{dt_unsortable_type_error}: When trying to sort/key unsupported types |
| 15 | + \item \code{dt_join_type_mismatch_error}: When column types are incompatible in joins/set operations |
| 16 | +} |
| 17 | +} |
| 18 | + |
| 19 | +\subsection{Backward Compatibility}{ |
| 20 | +All condition classes inherit from base R's condition system, so existing \code{tryCatch(..., error = ...)} code continues to work unchanged. The new classes simply provide more specific handling options when needed. |
| 21 | +} |
| 22 | +} |
| 23 | +\examples{ |
| 24 | + |
| 25 | +# Handle missing column errors specifically |
| 26 | +DT <- data.table(a = 1:3, b = 4:6) |
| 27 | +tryCatch({ |
| 28 | + setkey(DT, nonexistent_col) |
| 29 | +}, dt_missing_column_error = function(e) { |
| 30 | + cat("Missing column detected:", conditionMessage(e), "\n") |
| 31 | +}, error = function(e) { |
| 32 | + cat("Other error:", conditionMessage(e), "\n") |
| 33 | +}) |
| 34 | +} |
| 35 | +\seealso{ |
| 36 | +\code{\link{tryCatch}}, \code{\link{test}}, \url{https://adv-r.hadley.nz/conditions.html} |
| 37 | +For advanced exception handling patterns, see data.table's internal helper `tryCatch2` here - \url{https://github.com/jangorecki/logR/blob/master/R/logR.R} |
| 38 | +} |
0 commit comments