Learn the essential concepts of error handling in Ruby, including exceptions, the use of begin, rescue, and ensure blocks. These MCQs will help you understand Ruby’s error-handling mechanisms.
1. Understanding Errors and Exceptions
In Ruby, which of the following is used to handle errors and exceptions?
A) try
B) handle
C) rescue
D) catch
What is the difference between an error and an exception in Ruby?
A) Errors are always critical, while exceptions are recoverable
B) Errors are recoverable, while exceptions are always fatal
C) Exceptions are more difficult to handle than errors
D) Errors and exceptions are the same in Ruby
Which class does all exceptions in Ruby inherit from?
A) Exception
B) Error
C) StandardError
D) RuntimeError
What is the default behavior when an exception is not caught in Ruby?
A) It prints an error message and continues execution
B) It stops the program execution
C) It logs the exception to a file
D) It ignores the exception silently
What type of exception is raised if a method is called with wrong arguments in Ruby?
A) NoMethodError
B) ArgumentError
C) TypeError
D) RuntimeError
2. Using begin, rescue, and ensure
What is the purpose of the begin block in Ruby error handling?
A) To define the code that might raise an exception
B) To define the code to be executed after an exception is raised
C) To catch exceptions raised by other methods
D) To terminate the program if an error occurs
What does the rescue block do in Ruby?
A) It ensures that the code runs after the exception
B) It handles exceptions raised in the begin block
C) It prints a message when no exceptions occur
D) It raises a new exception after handling the old one
Which of the following is the correct syntax for rescuing an exception?
A) rescue => ExceptionType
B) rescue ExceptionType
C) begin rescue ExceptionType end
D) rescue from ExceptionType
What does the ensure block do in Ruby?
A) It runs the code only if an exception was raised
B) It runs the code after both begin and rescue
C) It runs only if the program has not executed successfully
D) It raises a new exception after handling the current one
In which order are begin, rescue, and ensure blocks executed in Ruby?
A) begin -> rescue -> ensure
B) begin -> ensure -> rescue
C) rescue -> begin -> ensure
D) ensure -> begin -> rescue
3. Advanced Error Handling
What happens if an exception is raised inside the ensure block?
A) The program terminates without handling the exception
B) The exception is passed to the rescue block
C) The exception is ignored and the program continues
D) The exception is raised after ensure execution completes
Can the rescue block handle multiple types of exceptions in Ruby?
A) No, it can only handle one exception at a time
B) Yes, using multiple rescue blocks
C) Yes, by specifying multiple exceptions in a single rescue statement
D) No, each exception needs its own begin block
What happens if no exception is raised in the begin block?
A) The rescue block is executed
B) The ensure block is executed
C) The program stops without executing the ensure block
D) The program throws a NoExceptionError
What does rescue => e do in Ruby?
A) It assigns the exception to the variable e
B) It prints the exception message
C) It causes the program to terminate
D) It re-raises the exception
How can you re-raise an exception after handling it in the rescue block?
A) Using raise with no arguments
B) Using re_raise keyword
C) Using retry statement
D) Using throw
4. Specific Exception Handling
Which of the following exception types is raised when an undefined method is called in Ruby?
A) NoMethodError
B) ArgumentError
C) NameError
D) RuntimeError
Which exception is raised when you attempt to divide by zero in Ruby?
A) ZeroDivisionError
B) ArgumentError
C) TypeError
D) StandardError
What is the correct syntax to handle a ZeroDivisionError exception in Ruby?
A) rescue ZeroDivisionError => e
B) rescue ZeroDivisionError
C) catch ZeroDivisionError
D) raise ZeroDivisionError
In Ruby, what is the class used for custom exception handling?
A) CustomError
B) StandardError
C) Exception
D) RuntimeError
How do you define a custom exception in Ruby?
A) class CustomError < StandardError; end
B) class CustomError < Exception; end
C) def CustomError; end
D) class CustomError: StandardError; end
5. Practical Examples of Error Handling
Which block is executed regardless of whether an exception is raised or not?
A) rescue
B) ensure
C) catch
D) raise
Can you nest begin blocks in Ruby?
A) No, Ruby only allows a single begin block
B) Yes, but only one rescue block is allowed
C) Yes, you can nest multiple begin blocks
D) Yes, but only within the ensure block
What is the output when the following Ruby code is executed?
rubyCopy codebegin
raise "Error!"
rescue => e
puts "Caught an exception: #{e.message}"
end
A) “Caught an exception: Error!”
B) “Error!”
C) nil
D) “Caught an exception: Exception”
Which of the following would you use to retry a block of code after an exception?
A) retry
B) retry_block
C) repeat
D) catch
What is the difference between raise and fail in Ruby?
A) raise creates a new exception, while fail prints an error message
B) fail is an alias for raise
C) raise can only be used with begin, while fail cannot
D) There is no difference
Which exception class is the superclass for all Ruby exceptions?
A) Exception
B) StandardError
C) RuntimeError
D) NoMethodError
How would you raise a custom exception with a message in Ruby?
A) raise CustomError.new(“Error message”)
B) raise “Error message”
C) raise CustomError, “Error message”
D) Both A and C
Can exceptions be handled asynchronously in Ruby?
A) Yes, by using threads and rescue blocks
B) No, Ruby does not support asynchronous exception handling
C) Yes, but only with external libraries
D) No, exceptions must always be synchronous
What would happen if the ensure block has an exception raised in Ruby?
A) The exception is ignored
B) The program terminates immediately
C) The exception is raised after the ensure block completes
D) The program continues executing with no interruption
Which of the following is an example of handling multiple exceptions in a single rescue block?
A) rescue ZeroDivisionError, ArgumentError
B) rescue [ZeroDivisionError, ArgumentError]
C) rescue ZeroDivisionError then ArgumentError
D) Both A and B
Answers Table:
Qno
Answer
1
C) rescue
2
A) Errors are always critical, while exceptions are recoverable
3
A) Exception
4
B) It stops the program execution
5
B) ArgumentError
6
A) To define the code that might raise an exception
7
B) It handles exceptions raised in the begin block
8
B) rescue ExceptionType
9
B) It runs the code after both begin and rescue
10
A) begin -> rescue -> ensure
11
A) The program terminates without handling the exception
12
C) Yes, by specifying multiple exceptions in a single rescue statement
13
B) The ensure block is executed
14
A) It assigns the exception to the variable e
15
A) Using raise with no arguments
16
A) NoMethodError
17
A) ZeroDivisionError
18
A) rescue ZeroDivisionError => e
19
C) Exception
20
A) class CustomError < StandardError; end
21
B) ensure
22
C) Yes, you can nest multiple begin blocks
23
A) “Caught an exception: Error!”
24
A) retry
25
B) fail is an alias for raise
26
A) Exception
27
D) Both A and C
28
A) Yes, by using threads and rescue blocks
29
C) The exception is raised after the ensure block completes