What is the difference between `raise “foo”` and `raise Exception.new(“foo”)`?

前端 未结 2 1627
醉酒成梦
醉酒成梦 2021-01-29 22:37

What is the difference - technical, philosophical, conceptual, or otherwise - between

raise \"foo\"

and

raise Exception.new(\"f         


        
2条回答
  •  一生所求
    2021-01-29 23:15

    From the offical documentation:

    raise   
    raise( string )
    raise( exception [, string [, array ] ] )
    

    With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil. With a single String argument, it raises a RuntimeError with the string as a message. Otherwise, the first parameter should be the name of an Exception class (or an object that returns an Exception when sent exception). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the rescue clause of begin...end blocks.

    raise "Failed to create socket"
    raise ArgumentError, "No parameters", caller
    

提交回复
热议问题