I often see custom Exception classes inheriting from StandardError.
Errors which you can generally deal with are subclassed from
StandardError:
module AbstractController
class Error < StandardError #:nodoc:
end
class ActionNotFound < StandardError #:nodoc:
end
unless action_name = method_for_action(action_name)
raise ActionNotFound, "The action ''#{action}'' could not
be found for
#{self.class.name}"
end
But why even bother to create the subclasses (which as shown above,
have no definition of themselves), when substituting ActionNotFound
with StandardError during the raise call will have the same effect.
--
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 https://groups.google.com/groups/opt_out.
I would understand if there was some level of customization involved
that distinguishes it from the super class:
class DoubleRenderError < Error
DEFAULT_MESSAGE = "Render and/or redirect were called multiple
times in this action. Please note that you may only call render OR
redirect, and at most once per action. Also note that neither redirect
nor render terminate execution of the action, so if you want to exit
an action after redirecting, you need to do something like
\"redirect_to(...) and return\"."
def initialize(message = nil)
super(message || DEFAULT_MESSAGE)
end
end
But in the exampels I provided in the initial post that was not the
case. ActionNotFound, for example, had no implementation of its own.
It seems to be a waste of memory allocation.
On Sep 16, 2:57 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org>
wrote:> I often see custom Exception classes inheriting from StandardError.
> Errors which you can generally deal with are subclassed from
> StandardError:
>
> module AbstractController
> class Error < StandardError #:nodoc:
> end
>
> class ActionNotFound < StandardError #:nodoc:
> end
>
> unless action_name = method_for_action(action_name)
> raise ActionNotFound, "The action ''#{action}''
could not be found for
> #{self.class.name}"
> end
>
> But why even bother to create the subclasses (which as shown above,
> have no definition of themselves), when substituting ActionNotFound
> with StandardError during the raise call will have the same effect.
--
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@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
On Sunday, September 16, 2012 7:57:44 PM UTC+1, John Merlino wrote:> > class ActionNotFound < StandardError #:nodoc: > end > > unless action_name = method_for_action(action_name) > raise ActionNotFound, "The action ''#{action}'' could not be found for > #{self.class.name}" > end > > But why even bother to create the subclasses (which as shown above, > have no definition of themselves), when substituting ActionNotFound > with StandardError during the raise call will have the same effect. >Because it doesn''t have the same effect. Raising specific subclasses allows you to write targeted rescue clauses rather than rescuing indiscriminately. Fred -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/IQOCofRA-TEJ. For more options, visit https://groups.google.com/groups/opt_out.
So we must build an entire class just for sake of more specific rescue
statements:
class MyError < StandardError
end
try_counter = 0
begin
try_counter += 1
puts ''Here 1''
raise MyError.new "Text" unless try_counter > 5
puts ''Here 2''
rescue MyError
puts ''Here 3 - MyError encountered''
retry
rescue StandardError
puts "Here 4 - Other error encountered (#{$!.inspect})" +
caller.inspect
raise
else
puts ''Here 5 - No errors''
ensure
puts ''Here 6 - Always done''
end
On Sep 16, 4:02 pm, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Sunday, September 16, 2012 7:57:44 PM UTC+1, John Merlino wrote:
>
> > class ActionNotFound < StandardError #:nodoc:
> > end
>
> > unless action_name = method_for_action(action_name)
> > raise ActionNotFound, "The action ''#{action}''
could not be found for
> > #{self.class.name}"
> > end
>
> > But why even bother to create the subclasses (which as shown above,
> > have no definition of themselves), when substituting ActionNotFound
> > with StandardError during the raise call will have the same effect.
>
> Because it doesn''t have the same effect. Raising specific
subclasses allows
> you to write targeted rescue clauses rather than rescuing indiscriminately.
>
> Fred
--
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@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.