×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Exception Handling in Ruby

By IncludeHelp Last updated : December 03, 2024

Exception Handling

Exceptions are abnormal conditions arising in the code segment at runtime in the form of objects. There are certain predefined Exception classes whereas we can create our exception known as Custom exception. Accepts distorts the normal flow of the program at runtime. Exception handling is the process of handling such abnormal conditions or you can also refer them as unwanted events. There is a distinct class known as Exception class in Ruby which contains method specially defined to deal with the unexpected events.

Syntax of Exception handling

To handle an exception, you can take help from raise and rescue block. The general syntax of Exception handling is given below:

begin
  raise
    # block where exception raise
  rescue
    # block where exception rescue
end

Code Demonstrating an Abnormal Condition

First let us see a code which is creating an abnormal condition:

a = 12
b = a/0

puts "Hello World"
puts b

The above Ruby code will give you the following error:

check.rb:7:in '/': divided by 0 (ZeroDivisionError)

Understanding Exception Behavior

You can observe that when an Exception is raised, it disrupts the flow of instructions and statements will not execute which are written after the same statement having Exception.

Handling Exceptions

We can modify the above code in the following way to avoid the execution distortion of the rest of the instructions.

=begin
Ruby program to show Exception Handling.
=end

begin
  a = 12
  
  raise
    b = a/0
  rescue 
    puts "Exception rescued"
    
  puts "Hello World"
  puts b
end

Output

Exception rescued
Hello World

Understanding Exception Behavior

In the above program, you can observe that we are writing the code inside the begin...end block. We have put the statement inside "raise" which may raise any kind of abnormality. We have used rescue statement to handle that exception. You can see that the exception raised is not affecting the rest of the instruction. We are getting "Hello World" printed at the end.

Creating Custom Exceptions

We can create our Exception known as Custom Exception with the help of any user defined method. Let us one of its examples for a better understanding of the implementation:

=begin
Ruby program to show Exception Handling.
=end

def exception_arised   
  begin
    puts 'Hello! Welcome to the Includehelp.com.'
    puts 'We are still safe from Exception'  
    # using raise to generate an exception 
    
    raise 'Alas! Exception Generated!'
    puts 'After Exception created'
    # using Rescue method to handle exception 
    
    rescue  
    puts 'Hurrah! Exception handled! We are safe now'
  end  
  puts 'We are out of begin!'  
end  

# invoking method 
exception_arised

Output

Hello! Welcome to the Includehelp.com. We are still safe from Exception
Hurrah! Exception handled! We are safe now
We are out of begin!

In the above code, we are creating our exception with the help of a user-defined method 'exception_arised'. We are then invoking it. The above is an example of Custom exception.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.