Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Python equivalent of Java's UnsupportedOperationException?

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?

like image 887
erwaman Avatar asked Jun 28 '16 03:06

erwaman


2 Answers

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.

like image 172
user2357112 supports Monica Avatar answered Oct 12 '22 12:10

user2357112 supports Monica


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.

like image 37
BrenBarn Avatar answered Oct 12 '22 13:10

BrenBarn