Rescue clause is used to handle the exceptions created by raise.
Ruby raise exception in initialize.
Kernel raise takes either exception class or exception instance.
Ruby s built in exceptions are great but they don t cover every possible use case.
The begin is omitted and the entire body of the method is subject to exception handling ending.
If we really need to emphasize oo way in raising exception as you claim i d rather add exception raise i e.
In this article we will look at.
In ruby like in most languages an exception is a way to convey that something went wrong.
By providing exception raise and discouraging kernel raise there s no sufficient way to raise pre allocated exceptions.
It is to be noted that the body of a method definition is an implicit begin end block.
Syntax begin rescue onetypeofexception rescue anothertypeofexception else other exceptions ensure always will be executed end.
To do exception handling we enclose the code that could raise an exception in a begin end block and use one or more rescue clauses to tell ruby the types of exceptions we want to handle.
While some languages only use exceptions for truly exceptional circumstances like run time errors ruby uses exceptions for a wide variety of errors and unexpected results.
None of ruby s standard exceptions fit so your best bet is to create a new kind of exception.
Ruby uses the kernel method termed as raise to create the exceptions which will be the instance of exception class or one of its subclass.