try-catch
try catch blocks for Ruby!
Try catch Block Chain use case
if you new to ruby and missing the try catch block here is a simple one for you
require 'try-catch'
__try__ { hello world }.catch{ "not hello world " }
#> "not hello world"
__try__ { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
#> there is and error, because undefined method `asdaf' for "hello world":String
#> you can cain up multiple catch for specific error
__try__ { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
puts __try__ { "hello world".asdaf }.catch(ArgumentError) { "it was and argument error" }.catch( NoMethodError ) { |ex| "bla bla #{ex}" }
#> "bla bla undefined method `asdaf' for "hello world":String"
puts __try_nc__ { "hello world".asd } #> non cached try block
puts __try_n__ { "hello".asd } || __try_n__{ "hello".downcase } #> "hello"
The each line style
__try__ { puts "hello world".asdf }
__catch__ { |ex| puts ex.to_s.upcase }
#> UNDEFINED METHOD `ASDF' FOR "HELLO WORLD":STRING
or in each line version with specific Exception case
__try__ { puts "hello world".asdf }
__catch__(ArgumentError) { |ex| puts ex.to_s.capitalize }
__catch__(NoMethodError) { |ex| puts ex.to_s.upcase }