简体   繁体   中英

exception handling in ruby on rails

I am a newbie to ruby on rails and developing some email apps, which uses AWS SES to send emails. I am uploading a csv file which contains only email address and an email will be sent to those email address.

Its a very basic app, which my app fails to send an email due to some reasons the app automatically stops sending emails. But I has to keep sending emails to the remaining email address.

How do I handle the exception. I have used ActionMailer.

Kindly Help me

def send_all_emails
  @emails.each do |email|
    send_one_mail email
  end
end

def send_one_mail email
  # your actual email sending code here
rescue
  # this will log error to Rails log, but will not halt the whole app
  Rails.logger.error $!
end

If you want to know about the exception,use

begin
 #some code here
rescue =>ex
 Rails.logger.error "#{ex.class.name} :  #{ex.message}"
end

ps: You can also use rescue Exception =>ex .But don't use it until needed.Since it will catch all minor exceptions like 'NoMemoryError' which we don't want.Use the first one,it will catch only the Standard errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM