In Rails.
Exception can rescue in controller class but in model class can not.
How to rescue exception in model?
You can do exception handling anywhere in a rails application, as it's part of Ruby, not part of Rails. Wherever you want to catch errors, just wrap it as so:
begin
SomethingElse.doSomething(x, y)
rescue Exception
ErrorLogger.log(Time.now, "Something went wrong!")
end
Please note that you should always "rescue Exception" instead of just "rescue". Just using "rescue" will only catch StandardError, which is a subclass of Exception (meaning something might get through that you don't want to get through).
Also as usual, you can raise an exception by doing:
raise ArgumentError, "Illegal arguments!"
anywhere in your code, be it a model or controller.
Unless I'm mistaken you can use error handling anywhere in Ruby. What are you trying to do?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With