Table of contents
1.
Introduction
2.
Throw and Catch in Ruby
3.
Example
3.1.
Code
3.2.
Output
3.3.
Explanation
4.
Frequently asked questions
4.1.
Ruby is which kind of programming language?
4.2.
Who invented Ruby and when?
4.3.
Is it necessary to use a catch block after a throw block?
4.4.
Do Throw and catch in ruby have the same utility in Java?
4.5.
In Which language Ruby is written?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Throw and Catch in Ruby

Author Ankit Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby's are expensive! To buy, one needs to earn a tremendous amount of money and to do that. We should get a good placement in a good company. Eventually, we need to learn the Ruby programming language to buy a ruby!

Fun Image

I hope you have already started learning Ruby, and in this article, we will carry forward your learnings. We will introduce you to the Throw and Catch in ruby, which can alter the flow of control.

Now without wondering more about buying Ruby, let us start learning the throw and catch in ruby.

Throw and Catch in Ruby

Do these keywords sound similar to you? In other programming languages like Java, python we have already used these keywords, but throw and catch in ruby is a different scenario Here these keywords have got nothing to do with handling exceptions or errors, and they are only used to alter the flow of control in loops of a Ruby program.

The Throw keyword or statement ends the program's flow and directly transfers the control outside of the catch block. The throw statement can cross multiple numbers of levels in the code and transfer the control outside of the desired catch block. And the Catch block is nothing but a labelled block of code which determines the control whenever a throw statement is executed. We need to label the throw statement as well. Let us understand this with the help of a Ruby program based on loops.

Note:- The throw statement also uses a label with it.

Example

Suppose we want to check whether any number is outside or inside our range. Here we can use the concept of throw and catch in ruby. Let us see the code for this example.

Code

# A program to use throw and catch in ruby
# for altering the flow of control
# defining a function

def less number(num)

# using the throw statement
# labelling the throw statement as numbererror
throw :numbererror if num < 10

# displaying the output when the throw is not being executed
puts "Number exceeds 10!"
end

# The catch block

 catch:numbererror do

# calling the function
lessnumber(91)
lessnumber(18)

# exits  the catch block here as throw will get executed 
lessnumber(4)
lessnumber(3)
end
puts "Outside the Catch Block"
You can also try this code with Online Ruby Compiler
Run Code

 

The above code will produce the following output 

Output

output

Let us now explain the above code

Explanation

In the above code for throw and catch in ruby, we have made a function named less number, in that we have used the throw statement to shift the control outside the catch block whenever the passed number is less than 10. When we passed 91 and 18  into our function, the output is ''Number exceeds 10" as the condition to trigger the throw statement is not fulfilled. But as soon as we pass a number less than 10, it shifts the control directly outside the catch block and prints the output.

So this was all about the use of throw and catch in the ruby programming language, hope you will have fun with it while coding!

Let us now see some of the frequently asked questions related to this.

Frequently asked questions

Ruby is which kind of programming language?

Ruby is a pure Object-oriented language, and it treats everything as an object. Even a class itself is an object that is an instance of the Class class.

Who invented Ruby and when?

Ruby was invented by a Japanese scientist and software developer Yukihiro Matsumoto in the mid of 1990s. 1993 to be precise.

Is it necessary to use a catch block after a throw block?

Yes! It is mandatory to use a catch block as it determines the shift of control of the flow after the execution of the throw block.

Do Throw and catch in ruby have the same utility in Java?

No! In Java, the try, throw and catch blocks are used to handle exceptions, whereas, in Ruby, it is used to shift the flow of control of the program.

In Which language Ruby is written?

It may come to your surprise as the Ruby language is itself written in C.

Let us now move to the conclusion part of this article.

Conclusion

In this article, we have extensively discussed the throw and catch  in ruby and its implementation in Ruby. We saw an example which was supported with a full-fledged code written in Ruby language to illustrate the throw and catch in Ruby.

The explanation of the code was also provided by us in order to make you crystal clear about the topic, and I hope it helped you!

You can explore some more articles on RubyFor loop in RubyOperators in Ruby or see the Documentation of ruby or access the Coding Ninjas Studio for our courses.

Happy Coding!

thankyou image
Live masterclass