Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The else Clause
2.1.
Syntax for the else clause
3.
Example
3.1.
Example 1
3.2.
Example 2
4.
Frequently Asked Questions
4.1.
What is an exception in ruby?
4.2.
How do you add exceptions to Ruby?
4.3.
What is an argument error?
4.4.
Why exception handling is needed?
4.5.
What is begin rescue in Ruby?
5.
Conclusion
Last Updated: Mar 27, 2024

The "else" clause helpful in exception handling in ruby

Author soham Medewar
0 upvote

Introduction

After all rescue clauses, a begin statement may include an else clause. The else clause, as you might expect, is a catch-all rescue, handling an exception that does not match a previous rescue clause. This isn't the purpose of anything else. If none of the rescue clauses are needed, the else clause is used instead. That is, if the code in the begin statement's body completes without exceptions, the code in the else clause is performed.

The else Clause

Adding code in an else clause is a lot like simply tacking it on to the end of the begin clause. The only difference is that when you use an else clause, the rescue statements do not handle any exceptions raised by that clause.

Syntax for the else clause

begin
# raise exception
rescue
# error handler (rescues the error)
else
# this block of code executes only if there is no exception
end
You can also try this code with Online Ruby Compiler
Run Code

Although the usage of an else clause is not extremely prevalent in Ruby, it can be artistically advantageous to emphasize the contrast between normal and exceptional completion of a block of code.

Note: Using an else clause without one or more rescue clauses is pointless. The Ruby interpreter permits it but warns you. After an else clause, no rescue clause is allowed.

Finally, the code in an else clause is performed only if the code in the begin clause completes and "falls off" the end. The else clause will obviously not be invoked if an exception occurs. However, break, next, return, and other similar expressions in the begin clause may prevent the else clause from being executed.

Example

Let us see some examples where you will understand the use of the else clause.

Example 1

Code

def someMethod
    begin
        raise 'This is an exception'
    rescue => e
        puts e.message
        puts 'We got an exception therefore the else block will not be executed'
    else
        puts 'As no exception is raised the else block will be executed'
    end
end
someMethod
You can also try this code with Online Ruby Compiler
Run Code

Output

This is an exception
We got an exception therefore the else block will not be executed
You can also try this code with Online Ruby Compiler
Run Code

Explanation

As we can see that we have raised an exception after the begin clause. Therefore, the rescue clause will get into action. As we have incurred an exception, the code in the else block will not be executed.

In the next example, we will see what happens when there is no exception raised.

Example 2

Code

def someMethod
    begin
        # No exception
    rescue => e
        puts e.message
        puts 'We got an exception therefore the else block will not be executed'
    else
        puts 'As no exception is raised the else block will be executed'
    end
end
someMethod
You can also try this code with Online Ruby Compiler
Run Code

Output

As no exception is raised the else block will be executed
You can also try this code with Online Ruby Compiler
Run Code

Explanation

When we have zero exceptions in our begin rescue block, the code inside the else clause gets executed.

Frequently Asked Questions

What is an exception in ruby?

Exception handling is a technique in Ruby that defines how to handle an error that is raised in a programme. An unwelcome or unexpected occurrence that occurs during the execution of a programme, i.e., at run time and disturbs the usual flow of the program's instructions, is an error in this context.

How do you add exceptions to Ruby?

Exceptions are basically classes. Simply build a class that inherits from StandardError or one of its descendants to generate a new type of exception. New exceptions have class names that finish in "Error" by convention.

What is an argument error?

ArgumentError is a descendant of the StandardError superclass that is triggered when parameters supplied to a method are erroneous, unexpected, or invalid in some way.

Why exception handling is needed?

Exception handling is critical because it ensures that the program's normal, desired flow is maintained even when unexpected events occur. Programs may crash or requests may fail if Java exceptions are not handled properly. Consumers may become irritated, and if this occurs frequently, you may lose those customers.

What is begin rescue in Ruby?

A possible exception could arise between "begin" and "rescue" in the code. The rescue block will be executed if an exception occurs.

Conclusion

In this article, we have discussed the else clause and how it is useful in exception handling in ruby.

After reading about the use of the else clause in exception handling, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see History of RubyRuby is an object-oriented language, and Ruby on rails.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

Happy Coding!

Live masterclass