How to rescue this exception

hi,i don’t know how to capture the following exception if the file
directory or name is not correct.
##########
def index
@csv_array=[]
begin
CSV.foreach(‘files/sample.csv’) do|row|
sub_array=row
@csv_array<<sub_array
end
rescue Exception=>e
flash.now[:error]=“error:#{e}”
raise
end
end
#########
after execute the above method,i get the exception like the
following,but i don’t like it.i don’t know how to capture the exception
and display a nice message within app’s layout.can you help me? thanks.

########
Errno::ENOENT in HomeController#index
No such file or directory - files/sample.csv
#########

You are re-raising the exception in rescue block. Try to modify the
rescue
block like this:

rescue Exception => e
flash.now[:error]=“error: #{e.message}”
end

You can do something like

raise “File not found” unless File.exists?(filename)

or better

begin

raise FileNotFoundException.new(“File not Found”) unless
File.exists?(filename)

rescue FileNotFoundException

end

by writing your own FileNotFoundException class and catching it in the
rescue class

[…]but i do not know why nearly
all the examples in the book re-raise the
exceptions.i think i have misunderstood the exception in ruby.if an
exception raises in the end used method,we generally do a final
capture,and no need to reraise,am i right?

You are right. Catching and re-raising the exception is used if you
want to do something when the exception happens but you don’t want to
fully handle the exception. For example, if you would like to log it
but you don’t want to be responsible for handling the problem. If in
your case catching the exception to update the flash is enough then
that’s all you need and you don’t have to re-raise.

pepe wrote in post #988286:

[…]but i do not know why nearly
all the examples in the book re-raise the
exceptions.i think i have misunderstood the exception in ruby.if an
exception raises in the end used method,we generally do a final
capture,and no need to reraise,am i right?

You are right. Catching and re-raising the exception is used if you
want to do something when the exception happens but you don’t want to
fully handle the exception. For example, if you would like to log it
but you don’t want to be responsible for handling the problem. If in
your case catching the exception to update the flash is enough then
that’s all you need and you don’t have to re-raise.

thank you,i have further understood exception under your help,which
makes me happy.

Szymon N. wrote in post #988178:

You are re-raising the exception in rescue block. Try to modify the
rescue
block like this:

rescue Exception => e
flash.now[:error]=“error: #{e.message}”
end

thank you,szimek,chaitanyv.it seems we just need to rescue the
exception,and no need to re-raise exception.but i do not know why nearly
all the examples in the book re-raise the
exceptions.i think i have misunderstood the exception in ruby.if an
exception raises in the end used method,we generally do a final
capture,and no need to reraise,am i right?

your case catching the exception to update the flash is enough then
that’s all you need and you don’t have to re-raise.

thank you,i have further understood exception under your help,which
makes me happy.

You’re very welcome. Glad I could help.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs