0
0
Rubyprogramming~5 mins

Exception hierarchy in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the root class for all exceptions in Ruby?
The root class for all exceptions in Ruby is <code>Exception</code>. All other exception classes inherit from it.
Click to reveal answer
intermediate
What is the difference between StandardError and Exception in Ruby?
<code>StandardError</code> is a subclass of <code>Exception</code> and is the default for rescue clauses. Most common errors inherit from <code>StandardError</code>, while <code>Exception</code> includes system errors that are usually not rescued.
Click to reveal answer
beginner
Name two common subclasses of StandardError.
Two common subclasses of StandardError are NoMethodError (raised when calling a method that doesn't exist) and ArgumentError (raised when wrong arguments are passed to a method).
Click to reveal answer
intermediate
Why should you avoid rescuing Exception directly in Ruby?
Rescuing Exception catches all exceptions, including system errors like Interrupt or SystemExit, which can prevent your program from exiting properly. It's safer to rescue StandardError or specific exceptions.
Click to reveal answer
intermediate
What class do syntax errors belong to in Ruby's exception hierarchy?
Syntax errors belong to the <code>SyntaxError</code> class, which inherits directly from <code>ScriptError</code>, a subclass of <code>Exception</code>. They are not subclasses of <code>StandardError</code>.
Click to reveal answer
Which class is the parent of most common Ruby exceptions?
AException
BStandardError
CRuntimeError
DScriptError
What happens if you rescue Exception in Ruby?
AYou rescue only syntax errors
BYou rescue only runtime errors
CYou rescue all exceptions, including system errors
DYou rescue only standard errors
Which class is NOT a subclass of StandardError?
ANoMethodError
BArgumentError
CRuntimeError
DSyntaxError
Which exception class is used for errors raised when a method is called with wrong arguments?
AArgumentError
BNameError
CTypeError
DNoMethodError
Which class is the direct parent of SyntaxError?
AScriptError
BStandardError
CException
DRuntimeError
Explain the Ruby exception hierarchy starting from Exception down to common error classes.
Think about which exceptions are rescued by default and which are not.
You got /4 concepts.
    Why is it recommended to rescue StandardError instead of Exception in Ruby?
    Consider what happens if you catch all exceptions including system interrupts.
    You got /3 concepts.