What is the best exception to use for bad Data State?

X

Xeno Campanoli

I don't want to use "ArgumentError" as this is a matter of data that may be
programmed in a daughter class, and is not necessarily an argument.
Specifically, the state of some object variables in this case.
 
R

Robert Klemme

I don't want to use "ArgumentError" as this is a matter of data that may be
programmed in a daughter class, and is not necessarily an argument.
Specifically, the state of some object variables in this case.

Can you provide more context information? When do you want to throw?

Kind regards

robert
 
X

Xeno Campanoli

Robert said:
Can you provide more context information? When do you want to throw?

Kind regards

robert

For now I am using "ScriptError". I was using "SyntaxError", but I think that
was just wrong. Here are some places:

I have what I want to be pure virtual methods in a base class:

def myPureVirtualMethod
raise ScriptError, "This should never be called. Daughter Classes MUST define
their own."
end

then there is

def validateThatThingMadeByPureVirtualMethodIsThere
unless theThingMMadeByPureVirtualMethodIsThere?
raise ScriptError, "That thingy isn't there."
end
end
 
B

Brian Candler

Xeno said:
def myPureVirtualMethod
raise ScriptError, "This should never be called. Daughter Classes
MUST define
their own."
end

raise NoMethodError perhaps?
def validateThatThingMadeByPureVirtualMethodIsThere
unless theThingMMadeByPureVirtualMethodIsThere?
raise ScriptError, "That thingy isn't there."
end
end

raise "That thingy isn't there" # RuntimeError

class MissingThingyErrror < RuntimeError; end
raise MissingThingyError, "That thingy isn't there"
 
B

Ben Bleything

For now I am using "ScriptError". =A0I was using "SyntaxError", but I thi= nk
that was just wrong. =A0Here are some places:

I have what I want to be pure virtual methods in a base class:

def myPureVirtualMethod
=A0 =A0 =A0 =A0raise ScriptError, "This should never be called. =A0Daught= er Classes
MUST =A0 =A0 =A0 =A0 define their own."
end

I use NotImplementedError for base methods in a "virtual" class.
def validateThatThingMadeByPureVirtualMethodIsThere
=A0 =A0 =A0 =A0unless theThingMMadeByPureVirtualMethodIsThere?
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0raise ScriptError, "That thingy isn't ther= e."
=A0 =A0 =A0 =A0end
end

Absolutely NotImplementedError here too.

Ben
 
C

Christopher Dicely

For now I am using "ScriptError". =C2=A0I was using "SyntaxError", but I = think
that was just wrong. =C2=A0Here are some places:

I have what I want to be pure virtual methods in a base class:

From what I've seen, it seems more idiomatic in Ruby not to define
"pure virtual" methods, and instead describe methods that must be
provided in child classes in the parent classes docs. Pure virtual
methods in static languages mostly exist to allow code to compile that
targets the generic interface of the class, but since Ruby doesn't
have static variable typing and compile-time checking that method
calls are valid for the type of the variable they are called against,
there doesn't seem to be a whole lot of reason to actually define a
pure virtual method.

If a call is made to an undefined method, you'll normally (unless
you've, e.g., done something with method_missing that redirects the
call) get a NoMethodError exception thrown.
 
A

Albert Schlef

Christopher said:
[in Ruby] there doesn't seem to be a whole lot of reason to
actually define a pure virtual method. [...]
instead describe methods that must be
provided in child classes in the parent classes docs

Documenting a method somewhere in the parent docs is like putting a
needle inside a haystack. You'll find it there only if you specifically
search for it. And you'll search for it only if you know it's supposed
to exist, and this is probably not going to happen.

Defining "abstract" (or "virtual") methods in Ruby is useful for
documentation.
 
A

Albert Schlef

Christopher said:
If a call is made to an undefined method, you'll normally (unless
you've, e.g., done something with method_missing that redirects the
call) get a NoMethodError exception thrown.

But NoMethodError is also raised if you have a typo in your code. So
when you're going to see it you're going to think that you've mistyped
something, or that your library isn't updated for Ruby 1.9, or has a
bug, or ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top