We use rescue clauses to inform Ruby what types of exceptions we want to manage, and we wrap the code that could raise an exception in a begin/end block.
By invoking Kernel#raise, Ruby really offers you the ability to manually raise exceptions. This gives you the option of selecting the type of exception to raise and even customising the error message. If you don't specify an exception type, Ruby will raise a RuntimeError by default (a subclass of StandardError ).
Ruby's technique of dealing with unforeseen events is through exceptions. You've seen exceptions in action if you've ever committed an error in your code, causing your application to crash with a message like SyntaxError or NoMethodError. When you throw an exception in Ruby, everything comes to a halt and your programme begins to shut down.
Exceptions are the results of breaking a rule. There are two types of exceptions:
Syntactic exceptions, which break the rules of grammar and syntax.
Semantic exceptions, which break the rules of meaning.
An exception is a signal that something has gone wrong in your code. You can create an exception by using the raise method.
Share a personalized message with your friends.