Reputation: 118261
Suppose I wrote the a code within a file named as "Sample.rb":
class Foo
def display()
p "hi"
end
end
Foo.new.display(1)
My program Sample.rb
definitely would be ended up,without performing any post execution which are also placed in that method,with the following error if don't handle it:
#ArgumentError: wrong number of arguments (1 for 0)
# from (irb):2:in `display'
# from (irb):6
# from C:/Ruby193/bin/irb:12:in `<main>'
Now coming to another point:
IRB
itself also a program. Which accepts the below code and also produce the same error as follow:
>> class Foo
>> def display()
>> p "hi"
>> end
>> end
#=> nil
>> Foo.new.display 1
#ArgumentError: wrong number of arguments (1 for 0)
# from (irb):2:in `display'
# from (irb):6
# from C:/Ruby193/bin/irb:12:in `<main>'
>>
Why in such a case IRB
itself doesn't exited abruptly, rather it's making such an Fatal error
as Non- fatal
and next >>
prompts
gives to us?
Upvotes: 0
Views: 167
Reputation: 9049
The error is in your program, and it throws an exception that Ruby understands (ArgumentError is an error that irb knows how to handle).
It would be a bad REPL if it dies everytime someone writes crappy code :)
Upvotes: 1
Reputation: 179392
It would suck if the interactive interpreter died as soon as you typed in something wrong. Therefore, irb
simply catches your errors, prints them out, and keeps on going until you quit. Technically, this is achieved by having a rescue
in the interpreter's main loop to catch your errors and continue processing your input.
A script without a rescue
block, on the other hand, will allow the exception to propagate all the way up until it hits the top level and kills the script. If you want the script to keep chugging along in some capacity, you will need to catch your exceptions using rescue
. It's desirable to have fatal errors in scripts, because it helps with debugging (for example, the PHP style "continue at all costs" tends to mask errors and make debugging more difficult than it needs to be.)
Upvotes: 2