Anybody using begin/rescue

i came from java world, and it is really big there.
i don’t see a lot in ruby code samples. just a little.

anybody out there using it? or is the errors just emailed
to you?

i came from java world, and it is really big there.
i don’t see a lot in ruby code samples. just a little.

anybody out there using it? or is the errors just emailed
to you?

We use it all the time to catch things and work around it…

You probably won’t see it in samples since samples tend not to include
much error handling…

It’s there though…

philip@MacBookPro:~/…/vendor/rails
$ grep -r “rescue” * | wc -l
502

-philip

When I started programming ruby I hardly ever used throw/rescue
sequences, but the more I develop and the more complex the backend
gets the more useful I find exceptions. Many things can be handled
with inline IF statements which i think helps to eliminate the need
for exceptions as much, but their are still places where its cleaner/
easier to throw an exception.

I use rescue excessively in my controller code. Even for something as
simple
as a show action:

def show
@thing = Thing.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:error] = “The thing you were looking for could not be found.”
redirect_to things_path
end

On Dec 18, 2007 8:24 AM, Andrew B. [email protected] wrote:

i don’t see a lot in ruby code samples. just a little.

philip@MacBookPro:~/…/vendor/rails
$ grep -r “rescue” * | wc -l
502

-philip


Ryan B.

Ryan B. wrote:

I use rescue excessively in my controller code. Even for something as
simple
as a show action:

def show
@thing = Thing.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:error] = “The thing you were looking for could not be found.”
redirect_to things_path
end


Ryan B.
http://www.frozenplague.net

well, dang nab. you don’t have a begin in that. is it required?
or is it a rescue in a def, then the def is the begin of code block?

i confused.

Hi –

On Tue, 18 Dec 2007, gemblon (t.b.) wrote:

redirect_to things_path
end


Ryan B.
http://www.frozenplague.net

well, dang nab. you don’t have a begin in that. is it required?
or is it a rescue in a def, then the def is the begin of code block?

The def carries an implicit begin.

David


Training for 2008!
Ruby on Rails training by David A. Black/Ruby Power and Light, LLC:
* Intro to Rails, New York, NY, February 4-7 2008
* Advancing With Rails, New York, NY, February 11-14 2008
Hosted by Exceed Education. See http://www.rubypal.com for details!

David A. Black wrote:

Hi –

On Tue, 18 Dec 2007, gemblon (t.b.) wrote:

redirect_to things_path
end


Ryan B.
http://www.frozenplague.net

well, dang nab. you don’t have a begin in that. is it required?
or is it a rescue in a def, then the def is the begin of code block?

The def carries an implicit begin.

David


Training for 2008!
Ruby on Rails training by David A. Black/Ruby Power and Light, LLC:
* Intro to Rails, New York, NY, February 4-7 2008
* Advancing With Rails, New York, NY, February 11-14 2008
Hosted by Exceed Education. See http://www.rubypal.com for details!

As in other object oriented languages, Ruby offers a mechanism for
exception handling. I When an exception occurs. Object of class
Exception created.

Raising Exception

begin
#process…
rescue
#handle errors
end
We enclose the code that could raise an exception in a begin/end block
and use rescue clauses to
tell Ruby the types of exceptions we want to handle.

Multiple Rescue

Begin

Process

rescue

handle error

rescue

handle error

end

You can have multiple rescue clauses in a begin block, and each rescue
clause can specify multiple exceptions to catch.

hope this helps

You can also use ensure to make sure certain things happen, no matter if
an
exception is raised or not.

On Dec 18, 2007 3:20 PM, Mohan K. [email protected]
wrote:

Ryan B.

Raising Exception
Multiple Rescue
clause can specify multiple exceptions to catch.

hope this helps

Posted via http://www.ruby-forum.com/.


Ryan B.