Ruby common exception

1, RuntimeError
Reason: This is the default exception raised by the raise method
raise


2, NoMethodError
Reason: An object is sent a message it can't resolve to a method name
a = Object.new
a.some_unkonw_method_name


3, NameError
Reason: The interpreter hits an identifier it can't resolve as a variable or method name
a = some_random_identifier


4, IOError
Reason: This is caused by reading a closed stream, writing to a read-only stream, and similar operations
STDIN.puts("Don't write to STDIN!")


5, Errno::error
Reason: This family of errors relates to file IO
File.open(-12)


6, TypeError
Reason: A method receives an argument it can't handle
a = 3 + "can't add a string to a number!"


7, ArgumentError
Reason: This is caused by using the wrong number of arguments
def m(x); end; m(1, 2, 3, 4, 5)

你可能感兴趣的:(Ruby)