Challenge: 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.
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
NegativeNumberErrorthat inherits fromException; - Write a function
check_positive_number(num)that raisesNegativeNumberErrorifnumis negative; otherwise, it returnsnum.
Example:
try:
result = check_positive_number(-5)
except NegativeNumberError:
print("You entered a negative number!")
Expected output:
You entered a negative number!
Soluzione
Grazie per i tuoi commenti!
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 6.67
Challenge: Creating and Using Custom Exceptions
Scorri per mostrare il menu
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.
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
NegativeNumberErrorthat inherits fromException; - Write a function
check_positive_number(num)that raisesNegativeNumberErrorifnumis negative; otherwise, it returnsnum.
Example:
try:
result = check_positive_number(-5)
except NegativeNumberError:
print("You entered a negative number!")
Expected output:
You entered a negative number!
Soluzione
Grazie per i tuoi commenti!
single