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.