Raising and Customizing Exceptions
Learn how to create and raise custom error messages to improve debugging.
Raising and Customizing Exceptions is a free Ruby Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 3 lessons in total.
Raising and Customizing Exceptions
Sometimes, you need to manually raise errors in your Ruby programs.
In this lesson, you will learn how to raise and customize exceptions to improve debugging.

Using raise
The raise keyword generates an exception.
raise 'Something went wrong!'Raising a Specific Exception
You can specify the type of exception you want to raise.
raise ArgumentError, 'Invalid argument provided!'Using raise with rescue
Handle raised exceptions using rescue.
begin
raise ZeroDivisionError, 'Cannot divide by zero!'
rescue ZeroDivisionError => e
puts "Error caught: #{e.message}"
endCreating Custom Exception Classes
Define your own exception classes by inheriting from StandardError.
class CustomError < StandardError
end
raise CustomError, 'This is a custom error!'Customizing Exception Messages
You can define custom messages and attributes for exceptions.
class CustomError < StandardError
def initialize(msg = 'Something went wrong')
super(msg)
end
end
raise CustomError.new('Custom error message!')Using begin - rescue - raise
Catch an error, log it, and then raise it again.
begin
raise 'An unexpected error!'
rescue => e
puts "Logging error: #{e.message}"
raise
endDefining Multiple Custom Exceptions
Create different types of exceptions for different errors.
class FileNotFoundError < StandardError; end
class PermissionDeniedError < StandardError; end
raise FileNotFoundError, 'File not found!'Congratulations!
You have learned how to raise and customize exceptions in Ruby.
Next, you will explore debugging techniques.

Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Raising and Customizing Exceptions” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Raising and Customizing Exceptions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 3 lessons in total.
What will I learn in “Raising and Customizing Exceptions”?
Learn how to create and raise custom error messages to improve debugging. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
How long does the “Raising and Customizing Exceptions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Handling Errors with begin and rescue
- Raising and Customizing Exceptions
- Debugging Techniques