Recall & Review
beginner
What is a custom exception class in Ruby?A custom exception class in Ruby is a user-defined class that inherits from the built-in Exception class or one of its subclasses. It allows you to create specific error types for your program to handle distinct error situations clearly.Click to reveal answer
beginner
How do you define a custom exception class in Ruby?You define a custom exception class by creating a new class that inherits from StandardError or Exception. For example:<br><pre>class MyError < StandardError; end</pre>Click to reveal answer
intermediate
Why should custom exceptions inherit from StandardError instead of Exception?
In Ruby, rescuing StandardError catches most common errors. Inheriting from StandardError ensures your custom exceptions behave like typical errors and are caught by default rescue blocks. Inheriting directly from Exception is discouraged because it can catch system errors that should not be rescued.
Click to reveal answer
beginner
How do you raise a custom exception in Ruby?
You raise a custom exception using the raise keyword followed by the exception class. For example:<br>
raise MyError, "Something went wrong!"
Click to reveal answer
beginner
What is the benefit of using custom exception classes?
Custom exception classes help you identify and handle specific error cases clearly. They make your code easier to debug and maintain by distinguishing different error types instead of using generic exceptions.
Click to reveal answer
Which class should a custom exception usually inherit from in Ruby?
✗ Incorrect
Custom exceptions should inherit from StandardError to be caught by typical rescue blocks and avoid catching system errors.
How do you raise a custom exception named MyError with a message?
✗ Incorrect
The correct syntax to raise a custom exception is using raise followed by the exception class and message.
What happens if you inherit a custom exception directly from Exception instead of StandardError?
✗ Incorrect
Inheriting from Exception can catch system errors like Interrupt, which should usually not be rescued.
Why use custom exception classes instead of generic exceptions?
✗ Incorrect
Custom exceptions help you handle specific errors clearly and improve code readability.
Which keyword is used to define a custom exception class in Ruby?
✗ Incorrect
You define a custom exception class using the class keyword followed by inheritance.
Explain how to create and use a custom exception class in Ruby.
Think about class inheritance, raising errors, and rescuing them.
You got /3 concepts.
Why is it important to inherit custom exceptions from StandardError instead of Exception?
Consider what kinds of errors you want to catch or avoid.
You got /3 concepts.