Puppet Function: try

Defined in:
lib/puppet/functions/try.rb
Function type:
Ruby 4.x API

Overview

try(Callable &$block)Any

Call a lambda and rescue exceptions from it.

Examples:

Using try/catch


# Prints the exception message from assert_type failure
notice(try() || {
  assert_type('String', 1)
}.catch |$exception| {
  $exception['message']
})

Parameters:

  • &block (Callable)

Returns:

  • (Any)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/functions/try.rb', line 12

Puppet::Functions.create_function(:try) do
  dispatch :try do
    block_param
  end

  def try
    [yield, nil]
  rescue
    [nil, { 'class' => $!.class.to_s, 'message' => $!.message, 'exception' => $! }]
  end
end