home PYTHONJAVA
 

Ruby Exception

Exceptions and execution are always tied together. If you open a file that doesn't exist and you don't handle it properly, then your program is considered low quality.

If an exception occurs, the program stops. Exceptions are used to handle various types of errors that can occur during program execution, so take appropriate action without letting the program stop completely.

Ruby provides a perfect mechanism for handling exceptions. We can attach code that might throw an exception to the begin/end block and tell Ruby exactly what type of exception to handle with the rescue clause.

Syntax

begin # Start raise.. # Throw an exception rescue [ExceptionType = StandardException] #Capture the specified type The default value of the exception is StandardException $! # indicates exception information $@ # indicates the location of the code where the exception occurred else #Other exceptions .. ensure #Enter the code block with or without exceptions end #End

Everything from begin to rescue is protected. If an exception occurs during the execution of the code block, control passes to the block between rescue and end.

For each rescue clause in the begin block, Ruby compares the thrown exception with each parameter in turn. If the exception named in the rescue clause is the same as the currently thrown exception type, or the parent of the exception, the match is successful.

If the exception does not match all of the specified error types, we can use an else clause after all rescue clauses.

Instance

#!/usr/bin/ruby begin file= open("/unexistant_file") if file puts "File opened successfully" end rescue file = STDIN end print file, "==", STDIN, "\n"

The above example runs the output as . As you can see, STDIN replaces file because open failed.

#<IO:0xb7d16f84>==#<IO:0xb7d16f84>

Using the retry statement

You can use the rescue block to catch exceptions and then use the retry statement to execute the begin block from the beginning.

Syntax

begin # The exception thrown by this code will be caught by the rescue clause below rescue # This block will catch all types of exceptions retry # This will move control to the beginning of begin end

Instance

#!/usr/bin/ruby begin file= open("/unexistant_file") if file puts "File opened successfully" end rescue fname= "existant_file" retry end

The following is the process flow:

  • An exception occurred while opening.
  • Jump to rescue. Fname is reassigned.
  • Jump through retry to the beginning of begin.
  • This file was successfully opened.
  • Continue the basic process.

Note: If the renamed file does not exist, this example code will try infinitely. So use retry with caution when handling exceptions.

Use the raise statement

You can throw an exception using the raise statement. The following method throws an exception when called. Its second message will be output.

Syntax

raise or raise "Error Message" or raise ExceptionType, "Error Message" or raise ExceptionType, "Error Message" condition

The first form simply rethrows the current exception (and throws a RuntimeError if there is no current exception). This is used in exception handlers that need to interpret exceptions before passing in exceptions.

The second form creates a new RuntimeError exception, setting its message to the given string. The exception is thrown to the call stack.

The third form creates an exception with the first argument and then sets the associated message to the second argument.

The fourth form is similar to the third form, and you can add any additional conditional statements (such as unless) to throw an exception.

Instance

#!/usr/bin/ruby begin puts 'I am before the raise.' raise 'An error has occurred.' puts 'I am after the raise.' rescue puts 'I am rescued.' end puts 'I am after the begin block.'

The above example runs the output as:

I am before the raise .
I am rescued.
I am after the begin block. 

Another example of a raise usage:

Instance

#!/usr/bin/ruby begin raise 'A test exception.' rescue Exception => e puts e.message puts e.backtrace.inspect end

The above example runs the output as:

A test exception.
["main.rb:4"]< /pre>

Using the ensure statement

Sometimes, whether or not you throw an exception, you need to ensure that some processing is done at the end of the code block. For example, you might have opened a file when you entered, and when you exit the block, you need to make sure to close the file.

The

ensure clause is this. Ensure is placed after the last rescue clause and contains a block of code that is always executed when the block terminates. It doesn't matter whether it terminates with a block, whether it throws and handles an exception, or if it terminates with an uncaught exception. The ensure block will always run.

Syntax

begin #.. Process #.. Throw an exception rescue #.. Handling errors ensure #.. Finally make sure to execute #.. This will always be performed end

Instance

begin raise 'A test exception.' rescue Exception => e puts e.message puts e.backtrace.inspect ensure puts "Ensuring execution" end

The above example runs the output as:

A test exception.
["main.rb:4"]
Ensuring execution

Use the else statement

If the else clause is provided, it is usually placed after the rescue clause, before any ensure.

The body of the

else clause is only executed if the code body does not throw an exception.

Syntax

begin #.. Process #.. Throw an exception rescue #.. Handling errors else #.. Execute if there are no exceptions ensure #.. Finally make sure to execute #.. This will always be performed end

Instance

begin # Throw 'A test exception.' puts "I'm not raising exception" rescue Exception => e puts e.message puts e.backtrace.inspect else puts "Congratulations-- no errors!" ensure puts "Ensuring execution" end

The above example runs the output as:

I'm not raising exception
Congratulations-- no errors!
Ensuring execution

Use the $! variable to catch the thrown error message.

Catch and Throw

Raise and rescue exception mechanisms can abandon execution when an error occurs, and sometimes need to jump out of some deeply nested structures during normal processing. At this point catch and throw come in handy.

catch defines a block that uses the given name (which can be a Symbol or a String) as a label. The block will execute normally until it encounters a throw.

grammar

throw :lablename #.. This will not be executed catch :lablename do #.. Encounter one throw After the match will be executed catch end or throw :lablename condition #..This will not be executed after a throw is encountered. catch :lablename do #.. The match will be executed after a throw is encountered catch end

Instance

In the example below, if the user types '!' to respond to any prompts, use a throw to terminate the interaction with the user.

Instance

def promptAndGet(prompt) print prompt res = readline.chomp throw :quitRequested if res == "!" return res end catch :quitRequested do name = promptAndGet("Name: ") age = promptAndGet("Age: ") sex = promptAndGet("Sex: ") # .. # Processing information end promptAndGet("Name:")

The above program requires human interaction and you can try it on your computer. The output of the above example runs 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 class. The next layer is in seven different types:

  • Interrupt
  • NoMemoryError
  • SignalException
  • ScriptError
  • StandardError
  • SystemExit

Fatal is another exception in this layer, but the Ruby interpreter only uses it internally.

ScriptError and StandardError have some subclasses, but we don't need to know these details here. The most important thing is to create our own exception classes, which must be subclasses of the class Exception or its children.

Let's see an example:

Instance

class FileSaveError < StandardError attr_reader :reason def initialize(reason) @reason = reason end end

Now, look at the example below, which will use the above exception:

Instance

File. Open(path, "w") do | file| begin # Write the data...< /span> rescue # An error has occurred raise FileSaveError.new($!) end end

The most important line here is raise FileSaveError.new($!). We call raise to indicate that an exception has occurred, passing it to a new instance of FileSaveError , which caused the data to fail due to a specific exception.






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.