Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Creating and Using Custom Exceptions | Custom Exceptions and Best Practices
Python Error Handling

bookChallenge: Creating and Using Custom Exceptions

Python's built-in exceptions handle many common error situations, but sometimes you need to identify and respond to specific conditions unique to your program. Custom exceptions give you the flexibility to define clear, meaningful error types that make your code easier to understand and maintain.

In this challenge, you will:

  • Define your own exception class for a specific error case;
  • Write a function that uses this custom exception to handle invalid input.

By practicing with custom exceptions, you gain valuable skills for building robust Python programs that communicate errors clearly and help others understand your code's intent.

Aufgabe

Swipe to start coding

Write a custom exception called NegativeNumberError. Then, implement a function named check_positive_number that takes a single argument. If the argument is a negative number, the function should raise your custom exception. Otherwise, it should return the number.

Requirements:

  • Define a custom exception class named NegativeNumberError that inherits from Exception;
  • Write a function check_positive_number(num) that raises NegativeNumberError if num is negative; otherwise, it returns num.

Example:

try:
    result = check_positive_number(-5)
except NegativeNumberError:
    print("You entered a negative number!")

Expected output:

You entered a negative number!

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you give an example of a custom exception in Python?

How do I define and use a custom exception in my own function?

What are some best practices for naming custom exceptions?

close

Awesome!

Completion rate improved to 6.67

bookChallenge: Creating and Using Custom Exceptions

Swipe um das Menü anzuzeigen

Python's built-in exceptions handle many common error situations, but sometimes you need to identify and respond to specific conditions unique to your program. Custom exceptions give you the flexibility to define clear, meaningful error types that make your code easier to understand and maintain.

In this challenge, you will:

  • Define your own exception class for a specific error case;
  • Write a function that uses this custom exception to handle invalid input.

By practicing with custom exceptions, you gain valuable skills for building robust Python programs that communicate errors clearly and help others understand your code's intent.

Aufgabe

Swipe to start coding

Write a custom exception called NegativeNumberError. Then, implement a function named check_positive_number that takes a single argument. If the argument is a negative number, the function should raise your custom exception. Otherwise, it should return the number.

Requirements:

  • Define a custom exception class named NegativeNumberError that inherits from Exception;
  • Write a function check_positive_number(num) that raises NegativeNumberError if num is negative; otherwise, it returns num.

Example:

try:
    result = check_positive_number(-5)
except NegativeNumberError:
    print("You entered a negative number!")

Expected output:

You entered a negative number!

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
single

single

some-alt