Ruby exception
Exceptions and execution are always associated. If you open a file that does not exist and do not handle the situation properly, your program is considered to be of low quality.
If an exception occurs, the program stops. Exceptions are used to handle various types of errors that may occur during program execution, so take appropriate action without causing the program to stop completely.
Ruby provides a perfect mechanism for handling exceptions. We can do it in begin/end
block with code that may throw an exception, and use the rescue
clause tells Ruby the perfect type of exception to handle.
Grammar
Begin # Start raising# Throw an exception, execute [ExceptionType=Standard
Exception] # The default value for capturing exceptions of the specified type is
StandardException $# Indicates exception information $@ # indicates the
code location where the exception occurred else # Other exceptions Ensure
#Enter the code block end # regardless of any exceptions
From begin
to rescue
Everything in is protected. If an exception occurs during the execution of the code block, control is passed to the rescue
and end
between the blocks.
For begin
Each in the block rescue
clause, Ruby takes turns comparing the thrown exception with each parameter. If rescue
the exception named in the clause is the same as the type of exception currentlythrown, or is the parent of the exception, the match is successful.
If the exception does not match all the specified error types, we can use all the rescue
clause with a else
clause.
Example
#!/usr/bin/rubybeginfile=open("/unexistant_file")iffileputs"File opened
successfully"endrescuefile=STDINendprintfile,"==",STDIN,"\\n"
The running output of the above example is. As you can see, STDIN
replaced file
because it failed to open
#<IO:0xb7d16f84>==#<IO:0xb7d16f84>
Use retry statement
You can use the rescue
block to catch an exception, and then use the retry
the statement is executed from the beginning begin
block.
Grammar
Begin # The exception thrown by this code will be resolved by the following
Clause capture rescue # This block will capture all types of exception retries
#This will move control to begin
The beginning end of
Example
#!/usr/bin/rubybeginfile=open("/unexistant_file")iffileputs"File opened
successfully"endrescuefname="existant_file"retryend
The following is the process flow:
An exception occurred when it was opened.
Skip to
rescue
.fname
is reassigned.Pass through
retry
skip tobegin
the beginning.The file was opened successfully this time.
Continue the basic process.
Note: if the renamed file does not exist, this example code will try indefinitely. So when handling exceptions, use them cautiously retry
.
Use raise
statement
You can use the raise
statement throws an exception. The following method throws an exception when called. Its second message will be output.
Grammar
raise or raise"Error Message" or raiseExceptionType,"Error
Message" or raiseExceptionType,"Error Message"condition
The first form simply re-throws the current exception (throwing a RuntimeError if there is no current exception). This is used in exception handlers that need to interpret the exception before passing in the exception.
The second form creates a new RuntimeError
exception, set its message to the given string. The exception is then thrown to the call stack.
The third form uses the first parameter to create an exception, and then sets the related message to the second parameter.
The fourth form is similar to the third form, where you can add any additional conditional statements, such as unless
to throw an exception
Example
#!/usr/bin/rubybeginputs'I am before the raise.'raise'An error has
occurred.'puts'I am after the raise.'rescueputs'I am rescued.'endputs'I
am after the begin block.'
The output of the above instance is as follows:
I am before the raise.
I am rescued.
I am after the begin block.
Another demonstration. raise
examples of usage:
Example
#!/usr/bin/rubybeginraise'A test
exception.'rescueException=>eputse.messageputse.backtrace.inspectend
The output of the above instance is as follows:
A test exception.
["main.rb:4"]
Use ensure
statement
Sometimes, whether or not an exception is thrown, you need to make sure thatsome processing is done at the end of the code block. For example, you may open a file when you enter, and when you exit the block, you need to make sure that the file is closed.
This is what the ensure
clause does. ensure
put it on the last one``rescue`` clause and contains a block of code that is always executed when the block terminates. It has nothing to do with whether the block exitsnormally, whether an exception is thrown and handled, or whether it is terminated because of an uncaught exception. ensure
block always runs.
Grammar
Begin # Process # Throw exception rescue # Processing error ensure
#Finally, ensure the execution of #
This always executes end
Example
beginraise'A test
exception.'rescueException=>eputse.messageputse.backtrace.inspectensureputs"Ensuring
execution"end
The output of the above instance is as follows:
A test exception.
["main.rb:4"]
Ensuring execution
Use else
statement
If provided else
clause, which is usually placed in the rescue
after the clause, any ensure
before.
The body of the else
clause executes only if the body of the code does not throw an exception.
Grammar
Begin # Process # Throw exception rescue # Processing error else #
If there are no exceptions, execute ensure # Finally, ensure the
execution of # This always executes end
Example
begin#Throw 'A test exception.'puts"I'm not raising
exception"rescueException=>eputse.messageputse.backtrace.inspectelseputs"Congratulations--
no errors!"ensureputs"Ensuring execution"end
The output of the above instance is as follows:
I'm not raising exception
Congratulations-- no errors!
Ensuring execution
Use $!
variables can catch thrown error messages.
Catch and Throw
raise
and rescue
exception mechanism can abort execution when an error occurs, and sometimes need to jump out of some deeply nested structureduring normal processing. At this time catch
and throw
it comes in handy.
catch
defines a block that uses a given name (which can be Symbol or String) as a label. Block will execute normally until one is encountered. throw
.
Grammar
Throw: lablename # This will not be executed by catch: lablenamedo
# When encountering a throw
The catchend or throw that will be executed after matching: lablenamecondition #
This will not be executed by catch: lablenamedo # Matching will be executed after encountering a throw
Catchend
Example
In the following example, if the user types '!'
in response to any prompts, use a throw
terminates interaction with the user.
Example
defpromptAndGet(prompt)printpromptres=readline.chompthrow:quitRequestedifres=="!"
returnresendcatch:quitRequesteddoname=promptAndGet("Name:")age=promptAndGet("Age:")
sex=promptAndGet("Sex:")#..#process information endpromptAndGet("Name:")
The above program requires human interaction, and you can try it on your computer. The output of the above instance is as follows:
Name: Ruby on Rails
Age: 3
Sex: !
Name:Just Ruby
Class Exception
Ruby’s standard classes and modules throw exceptions. All exception classes form a hierarchy, including the top Exception
including the class. The next layer is seven different types:
Interrupt
NoMemoryError
SignalException
ScriptError
StandardError
SystemExit
Fatal
is another exception in this layer, but the Ruby interpreter uses it only internally.
ScriptError
and StandardError
there are some subclasses, but we don’t need to know these details here. The most important thing is to createour own exception classes, which must be classes Exception
or a subclass of its descendants.
Let’s look at an example:
Example
classFileSaveError<StandardErrorattr_reader:reasondefinitialize(reason)@reason=reasonendend
Now, looking at the following example, you will use the above exception:
Example
File.open(path,"w")do\|file\|begin# Write data
...rescue# An error occurred raiseFileSaveError.new($!)endend
The most important line here is raise FileSaveError.new($!)
. We call raise
to indicate that the exception has occurred and pass it on to FileSaveError
fails to write data due to a specific exception