Ruby - SPLessons

Ruby Exception Handling

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby Exception Handling

Ruby - Handling Exceptions

shape Introduction

This chapter demonstrates about the Ruby Exception Handling which is the specific process to deals with errors in a manageable and predictable way and following are the concepts covered in this chapter.
  • Exceptions
  • Exception Class

Exceptions

shape Description

The execution and the exception always go together. In the event when user trying to open one file, which is not exist, then user didn't handle this circumstance appropriately, then the program quality is not good then the program stops its execution when an exception occurs. So these exceptions can be used to handle the different types of errors, which is happen amid executing program and make proper action inplace of stopping current program totally. Ruby give a pleasant component to handle raised exceptions which generate a snippet in begin/end blocks to raise the exception and user need to use rescue classes to demonstrates the different ruby exceptions if user needs to handle. The snippet below demonstrates the Handling Exceptions. [ruby] begin # - rescue OneTypeOfException # - rescue AnotherTypeOfException # - else # Other exceptions ensure # Always will be executed end [/ruby] In the program everything is protected with the rescue from beginning if any exception occurs during the block execution control is passed between the rescue and end. The code below demonstrates to handle the exceptions as shown. [ruby] begin file = open("/unexistant_file") if file puts "File opened successfully" end rescue file = STDIN end print file, "==", STDIN, "\n" [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. retry By using the rescue user can capture the exception and then retry statement will be execute at the beginning of the begin block. The snippet below demonstrates the retry statement as shown below. [ruby] begin # Exceptions raised by this code will # be caught by the following rescue clause rescue # This block will capture all types of exceptions retry # This will move control to the beginning of begin end [/ruby] The code below demonstrates the retry statement as shown below. [ruby] begin file = open("/unexistant_file") if file puts "File opened successfully" end rescue fname = "existant_file" retry end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. Catch and Throw If an exception system of raise and rescue is awesome for seeking execution of the things are going wrong, it's occasionally pleasant to have the capacity to bounce out of some deeply nested construct during the processing. which is the place for catch and through prove to be useful. The catch characterizes a piece which is marked with the given name. The block will execute until user get a throw the syntax below demonstrates the through and catch. [ruby] throw :lablename #.. this will not be executed catch :lablename do #.. matching catch will be executed after a throw is encountered. end OR throw :lablename condition #.. this will not be executed catch :lablename do #.. matching catch will be executed after a throw is encountered. end [/ruby] The code below demonstrates the Catch and Throw as shown below. [ruby] 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: ") # .. # process information end promptAndGet("Name:") [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Exception Class

shape Description

Ruby's standard modules and classes will raise an exceptions. All exceptions are class frame a chain of importance, at the top with class Exception. The following level contains seven types which are shown below. One more exception is there in which Fatal, level yet Ruby interpreter just uses inside Both ScriptError and StandardError those are have various sub classes, however which doesn't have interest points of here but the important thing is an exception class, they should be sub classes of either Exception class or mostly its relatives. The code below demonstrates the class exceptions as shown below. [ruby] class FileSaveError < StandardError attr_reader :reason def initialize(reason) @reason = reason end end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Summary

shape Key Points

  • retry statement is used to execute the begin block from beginning.
  • Exception is an specific process to deals with error.
  • Execution and exception always together.