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 ######### -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Szymon Nowak 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}" > endthank 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 <programming ruby> 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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> [...]but i do not know why nearly > all the examples in the book <programming ruby> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe wrote in post #988286:>> [...]but i do not know why nearly >> all the examples in the book <programming ruby> 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> >> [...]but i do not know why nearly > >> all the examples in the book <programming ruby> 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.You''re very welcome. Glad I could help. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.