Ramda.js R.tryCatch() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

A helper that wraps a risky function. The new function runs the tryer; if the tryer throws, it runs the catcher and returns that result instead — a try...catch block packaged as a reusable function.

Status: stable since Ramda v0.20.0 — not deprecated, experimental, or non-standard.

Syntax

Call R.tryCatch. Start with this line.

Ramda
R.tryCatch(tryer, catcher)
CallWhat it does
R.tryCatch(tryer, catcher)Primary signature.
R.tryCatch(...)Call the function helper.

Prefer import { tryCatch } from "ramda" when you want a named import.

Library declarations
Ramda
R.tryCatch(tryer, catcher)

Parameters

ParameterDescription
tryerRequired. The function that may throw
catcherRequired. Called as catcher(err, ...args) only when tryer throws

Return Value

Return ValueDescription
*A new function that takes the same arguments as tryer

Ramda Examples

Example 1 — Official: No Throw

When the tryer succeeds, the catcher never runs.

Ramda
const readX = R.tryCatch(R.prop("x"), R.F);

console.log(readX({ x: true }));
// → true
Try It Yourself

How It Works

  1. Tryer. R.prop("x") reads x from the object.
  2. Success. { x: true } has x, so it returns true.
  3. Catcher idle. R.F (always false) is never called.

Example 2 — Official: Recover from a Throw

The tryer throws, so the catcher’s value is returned.

Ramda
const safe = R.tryCatch(
  () => { throw "foo"; },
  R.always("caught")
);

console.log(safe("bar"));
// → "caught"
Try It Yourself

How It Works

  1. Call. safe("bar") runs the tryer with "bar".
  2. Throw. The tryer throws "foo".
  3. Catch. R.always("caught") runs and ignores its arguments.
  4. Result. "caught" is returned like any normal value.

Example 3 — Official: Fallback of the Same Type

R.times throws on a bad count; the catcher returns an empty array.

Ramda
const range = R.tryCatch(R.times(R.identity), R.always([]));

console.log(range(3));
// → [0, 1, 2]

console.log(range("s"));
// → []
Try It Yourself

How It Works

  1. Valid. R.times(R.identity)(3) builds [0, 1, 2].
  2. Invalid. "s" isn’t a count, so R.times throws a RangeError.
  3. Fallback. R.always([]) returns an empty array.
  4. Same type. Both paths give an array, so later steps can always use array methods.

Example 4 — Official: Catcher Gets (err, value)

The catcher receives the error first, then the original arguments.

Ramda
const check = R.tryCatch(
  () => { throw "this is not a valid value"; },
  (err, value) => ({ error: err, value })
);

console.log(check("bar"));
// → { error: "this is not a valid value", value: "bar" }
Try It Yourself

How It Works

  1. Throw. The tryer throws a message string.
  2. First argument. The catcher gets that message as err.
  3. Second argument. The original input "bar" arrives as value.
  4. Report. The catcher returns both — handy for logging what failed and why.

Example 5 — Safe JSON.parse in a Pipe

Wrap a throwing step so the rest of the pipe always gets an object.

Ramda
const safeParse = R.tryCatch(JSON.parse, R.always({}));
const getCount = R.pipe(safeParse, R.propOr(0, "count"));

console.log(getCount('{"count":3}'));
// → 3

console.log(getCount("not json"));
// → 0
Try It Yourself

How It Works

  1. Wrap. safeParse returns the parsed value, or {} if JSON.parse throws.
  2. Pipe. R.pipe sends that object to the next step.
  3. Read. R.propOr(0, "count") gives count, or 0 if it’s missing.
  4. Result. Valid JSON → 3; bad JSON → {} → 0. No crash either way.

Browser & Runtime Support

R.tryCatch has been in Ramda since v0.20.0. Ramda’s browser build is plain ES5, so it runs in any modern browser and in Node.js / Bun / Deno with npm install ramda.

✓ Stable · Ramda 0.20.0+

R.tryCatch()

Built on JavaScript’s own try...catch, so no special browser API is needed. The examples use ES2015 arrow functions, supported by every evergreen browser.

100% With Ramda 0.20.0+
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.tryCatch Ramda 0.20.0+

Bottom line: Safe for production with Ramda 0.20.0 or newer. Remember it handles synchronous throws only. The labs on this page use [email protected].

Frequently Asked Questions

R.tryCatch(tryer, catcher) returns a new function. It runs tryer with your arguments; if tryer returns normally, you get that result. If tryer throws, catcher runs instead and its return value is the result. Docs sample: R.tryCatch(R.prop('x'), R.F)({ x: true }) //=> true.
A try...catch block is a statement you write in place. R.tryCatch builds a reusable function, so you can name it, pass it around, and drop it straight into R.pipe or R.map.
The error first, then every original argument: catcher(err, ...args). That lets a catcher log the error and still use the input, e.g. (err, value) => ({ error: err, value }).
No. It only catches errors thrown synchronously. An async tryer returns a Promise, and if that Promise rejects, the catcher is never called. Use R.otherwise or .catch() for Promises.
The Ramda docs say both should return the same type for effective composition. If success gives an object and failure gives {}, the next pipe step can handle both without extra checks.
No. R.tryCatch has been a stable Ramda function helper since v0.20.0 — not deprecated, experimental, or non-standard.

Next: R.unapply()

Learn how to gather a function’s arguments into one array.

R.unapply() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful