0Pricing
Ruby Academy · Lesson

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 — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

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.

Raising and Customizing Exceptions — illustration 1

2

Using raise

The raise keyword generates an exception.

raise 'Something went wrong!'

3

Raising a Specific Exception

You can specify the type of exception you want to raise.

raise ArgumentError, 'Invalid argument provided!'

4

Using raise with rescue

Handle raised exceptions using rescue.

begin
  raise ZeroDivisionError, 'Cannot divide by zero!'
rescue ZeroDivisionError => e
  puts "Error caught: #{e.message}"
end

5

Creating Custom Exception Classes

Define your own exception classes by inheriting from StandardError.

class CustomError < StandardError
end

raise CustomError, 'This is a custom error!'

6

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!')

7

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
end

8

9

Defining Multiple Custom Exceptions

Create different types of exceptions for different errors.

class FileNotFoundError < StandardError; end
class PermissionDeniedError < StandardError; end

raise FileNotFoundError, 'File not found!'

10

Congratulations!

You have learned how to raise and customize exceptions in Ruby.

Next, you will explore debugging techniques.

Raising and Customizing Exceptions — illustration 10

Frequently asked questions

Is the “Raising and Customizing Exceptions” lesson free?

Yes — the full text of “Raising and Customizing Exceptions” is free to read here on the web, and the Ruby Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

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

  1. Handling Errors with begin and rescue
  2. Raising and Customizing Exceptions
  3. Debugging Techniques
← Back to Ruby Academy