Safe call - Tki-sor/UtilJS GitHub Wiki

UtilJS implements the runCatching method, used as follows:

// Ensures the code won't terminate due to exceptions  
let rc = UtilJS.control.runCatching(() => {  
    // In kubejs, the number 1 would be recognized as 1.0 and cause failure,  
    // so the string "1" should be passed instead  
    Integer.valueOf(1)  
    return "OK"  
})  
  
// Then you can get the return value through result; here value should be null  
rc.getValue()  
rc.getError()  

Result is a wrapper for the result and error, similar to what Kotlin or Rust use.

If you only care about the result, you can directly call result.getValue(), but note that the result might be null — after all, the code you wrap might not always succeed.

Additionally, you can check if execution succeeded with isSuccess() or failed with isFailure().