# Rambling: try/catch

**URL:** <https://www.ruby-forum.com/t/rambling-try-catch/119540>\
**Category:** Ruby\
**Created:** [November 7, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/rambling-try-catch/119540 "2007-11-07T20:18:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/rambling-try-catch/119540/1 "2007-11-07T20:18:02Z")

</div>

Just had an vague idea about generic java “try-catch” converted to ruby-  
world I would like some feedback on.

I’m building a “microscopic CMS” system to learn more ruby, and of  
course  
it would be good to implement proper error catching system. Try-catch is  
what java uses.

Now the vague idea was to create one error catching function, and to  
throw all things that might fail (say loading a file) “through” that  
function. Passed as a code block? I am not sure, it keeps slipping from  
me. 🙂

What do you think?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/rambling-try-catch/119540/2 "2007-11-07T20:25:40Z")

</div>

On Nov 7, 12:40 pm, Casimir P [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> me. 🙂
> 
> What do you think?
> 
> –  
> Casimir P.  
> Art Portfolio:[http://csmr.dreamhosters.com](http://csmr.dreamhosters.com)

I’m not sure I completely understand (I’ve had a string of wrong  
answers here recently), but I think you’re looking for begin/rescue:

begin  
do\_something\_that\_might\_explode()  
rescue =\> e  
puts “uh-oh, an exception: #{e}: #{e.backtrace.join(”\n")}"  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/rambling-try-catch/119540/3 "2007-11-07T21:17:46Z")

</div>

On Nov 7, 2007, at 1:45 PM, Casimir P wrote:

> Now the vague idea was to create one error catching function, and to  
> throw all things that might fail (say loading a file) “through” that  
> function. Passed as a code block? I am not sure, it keeps slipping  
> from  
> me. 🙂

I **think** this is what you’re looking for:

begin

# do something useful…

rescue =\> e

# do something with the error ‘e’

end

To keep rescue code short, you could have a function:  
error\_function = lambda { | error |  
#do something useful…  
}

and then for rescue…

begin  
…  
rescue =\> e  
error\_function[e]  
end

Tadah!

HTH,  
ari

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/rambling-try-catch/119540/4 "2007-11-07T22:59:22Z")

</div>

Casimir P [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> me. 🙂
> 
> What do you think?
> 
> –  
> Casimir P.  
> Art Portfolio: [http://csmr.dreamhosters.com](http://csmr.dreamhosters.com)

The idea of passing around a signal handler is not new.  
It allows you to handle low-level condition in a high-level manner.

Here is one, done Ã la SRFI-34.

def with\_exception\_handler(k = nil)  
begin  
yield  
rescue Exception =\> e  
if k then if k.call(e) == :retry then retry end end  
end  
end

limited\_patience\_handler =  
lambda {  
# can be called to handle exception three times.  
# after that it loses patience and just reraise things.  
i=[1,2,“last one”]  
lambda{|e| if not i.empty?  
puts “Got exception ##{i.shift}: #{e.inspect}”  
:retry  
else  
puts “I’m done covering for you. Reraising to my  
supervisor”  
raise e  
end}}.call

with\_exception\_handler(limited\_patience\_handler) {  
puts “Raising”  
raise “Test exception”  
puts “Never reach here”  
}

\*\*\*\*Output:

Raising  
Got exception #1: #\<RuntimeError: Test exception\>  
Raising  
Got exception #2: #\<RuntimeError: Test exception\>  
Raising  
Got exception #last one: #\<RuntimeError: Test exception\>  
Raising  
I’m done covering for you. Reraising to my supervisor  
/tmp/x.rb:24: Test exception (RuntimeError)  
from /tmp/x.rb:3:in `with\_exception\_handler’  
from /tmp/x.rb:22

YS
