class BillRowError < StandardError
def initialize(field, index) @field = field @index = index
end
end
I'll call this like so:
raise(BillRowError.new(:roamingcalls, @index), "Roaming Calls field
missing") if n.length == 0
But now I'd like to be able to modify the string that Ruby prints when the
exception is not rescue'd. I thought I could add this method to the
BillRowError class:
def message @message + " field: #{@field}, row: #{@index}"
end
That almost works but I get a "instance variable @message not initialized"
warning, which means Ruby is not setting @message in my object like I
expected. Making my own message= method doesn't help.
Can an exception object access and modify the message that gets passed in
the "raise"?
class BillRowError < StandardError
def initialize(field, index) @field = field @index = index
end
end
I'll call this like so:
raise(BillRowError.new(:roamingcalls, @index), "Roaming Calls field
missing") if n.length == 0
But now I'd like to be able to modify the string that Ruby prints when the
exception is not rescue'd. I thought I could add this method to the
BillRowError class:
def message @message + " field: #{@field}, row: #{@index}"
end
That almost works but I get a "instance variable @message not initialized"
warning, which means Ruby is not setting @message in my object like I
expected. Making my own message= method doesn't help.
Can an exception object access and modify the message that gets passed in
the "raise"?
Judging by the C source, Exception uses hidden(*) instance variables
"mesg" and "bt" for message and backtrace respectively. However, the
accessor #message internally calls #to_s, which you can override.
class BillRowError < StandardError
def initialize(field, index) @field = field @index = index
end
alias :orig_to_s :to_s
def to_s
"#{orig_to_s} field: #{@field}, row: #{@index}"
end
end
begin
raise BillRowError.new(5, 7), "bar"
rescue BillRowError => e
p e
p e.instance_variables
p e.message
p e.backtrace
end
The default Exception:initialize is defined to take 1 parameter, a
string containing the error message
You define different initialize parameters so @message is not getting
set as expected
Maybe something like:
class BillRowError < StandardError
def initialize(msg, field, index)
super(msg) @field = field @index = index
end
def message @message + " field: #{@field}, row: #{@index}"
end
end
...
raise(BillRowError.new("Roaming Calls field missing",:roamingcalls, @index), ) if n.length == 0
[Note: parts of this message were removed to make it a legal post.]
Hi everyone
I want to make a custom exception like so:
class BillRowError < StandardError
def initialize(field, index) @field = field @index = index
end
end
I'll call this like so:
raise(BillRowError.new(:roamingcalls, @index), "Roaming Calls field
missing") if n.length == 0
But now I'd like to be able to modify the string that Ruby prints when the
exception is not rescue'd. I thought I could add this method to the
BillRowError class:
def message
@message \+ " field: \#\{@field\}, row: \#\{@index\}"
end
That almost works but I get a "instance variable @message not initialized"
warning, which means Ruby is not setting @message in my object like I
expected. Making my own message= method doesn't help.
Can an exception object access and modify the message that gets passed in
the "raise"?
Wow thanks for this, I didn't think of aliasing the old to_s. I find it
strange that exceptions don't just use a plain old @message instance
variable but I suppose there's some good reason for it.
Judging by the C source, Exception uses hidden(*) instance variables
"mesg" and "bt" for message and backtrace respectively. However, the
accessor #message internally calls #to_s, which you can override.
class BillRowError < StandardError
def initialize(field, index) @field = field @index = index
end
alias :orig_to_s :to_s
def to_s
"#{orig_to_s} field: #{@field}, row: #{@index}"
end
end
begin
raise BillRowError.new(5, 7), "bar"
rescue BillRowError => e
p e
p e.instance_variables
p e.message
p e.backtrace
end
I should stop answering questions based on reading the docs at ruby- docs.org, they are just not very clear...plus sloppy reading on my
part doesn't help
So message is not an attribute so using @message is wrong.
It looks like overriding to_s, or maybe using super in the override of
message to get the parents input.
I should stop answering questions based on reading the docs at ruby- docs.org, they are just not very clear...plus sloppy reading on my
part doesn't help
So message is not an attribute so using @message is wrong.
It looks like overriding to_s, or maybe using super in the override of
message to get the parents input.