Home » Ruby programming

Exception Handling in Ruby

Ruby exception handling: In this tutorial, we are going to learn about the exception handling with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on September 26, 2019

Ruby 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.

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

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)

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.

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

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.

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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.