I'm looking at Python's built-in exceptions and wondering what the closest equivalent of Java's UnsupportedOperationException
is. NotImplementedError
is close but seems to suggest something slightly different. Should I be using RuntimeError
or implementing my own Exception
?
The closest equivalent is to simply not implement the unsupported method. The resulting exception if you try to use the nonexistent method is an AttributeError
.
I don't know Java, but looking at what you linked to (and a few examples online), I'd say there is no single equivalent. That is quite a broad description for an exception type.
If the operation isn't supported because the types of the operands are wrong, you'd use TypeError. If it isn't supported because the values are incompatible somehow, you'd use ValueError. Or, as user2357112 notes, you would just do nothing and get an AttributeError when you tried to access a nonexistent method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With