简体   繁体   English

了解UnsupportedOperationException

[英]Understanding UnsupportedOperationException

I don;t quite understand where I can throw this exception. 我不太明白我可以在哪里抛出这个异常。

For instance, I'm implementing the Future<T> interface and don't want anyone calls the method: 例如,我正在实现Future<T>接口,并且不希望任何人调用该方法:

Future#get(long, TimeUnit) . 未来#get(long,TimeUnit)

So, can I just throw the UnsupportedOperationException ? 那么,我可以抛出UnsupportedOperationException吗?

public T get(long timeout, TimeUnit unit){
    throw new UnsupportedOperationException();
}

The thing is the specification of the method doesn't say anything about throwing the Exception. 事情是该方法的规范没有说什么关于抛出异常。 The Exception, in turn 反过来,例外

throws to indicate that the requested operation is not supported. 抛出以指示不支持所请求的操作。

Class UnsupportedOperationException Class UnsupportedOperationException

I mean, is it common to throw it if you don't want it to be called or it may be considered incorrect because not all methods've been implemented? 我的意思是,如果您不希望它被调用或者可能被认为是不正确的,因为并非所有方法都已实现,抛出它是否常见? In my specific case, I don't think that calling the method would make sense... 在我的具体情况下,我认为调用该方法没有意义......

Technically UnsupportedOperationException is unchecked, and therefore can be thrown anywhere you like. 技术上UnsupportedOperationException未选中,因此可以在任何您喜欢的地方抛出。 However throwing it in unexpected places will cause your class to be less easy to use, and is not recommended. 但是将它扔到意想不到的地方会导致你的课程不那么容易使用,不推荐使用。

The place where UnsupportedOperationException is expected to be thrown is in "optional operations". 预期将抛出UnsupportedOperationException的位置在“可选操作”中。 The Java framework contains plenty of these, especially in the Collections framework. Java框架包含大量这些内容,尤其是在Collections框架中。 For example "add" is an optional operation , because immutable collections should not allow it. 例如, “add”是一个可选操作 ,因为不可变集合不应该允许它。 Throwing UnsupportedOperationException is exactly what you should do if you don't want to write one of these methods. 如果您不想编写其中一种方法,则抛出UnsupportedOperationException正是您应该执行的操作。

In your case timed "get" is pretty fundamental to the use of Future, and you will cause some surprise if you don't implement it. 在你的情况下,定时“获取”对于使用Future非常重要,如果你不实现它会引起一些惊喜。 if you are going to do that, make sure it is well documented, and be aware that this will cause your implementation of Future to be unusable in some cases, and to potentially cause a program that uses it to crash. 如果你打算这样做,请确保它有详细记录,并注意这将导致你的Future实现在某些情况下无法使用,并可能导致使用它的程序崩溃。

If you simply don't have the resources to write a timed get for your implementation of Future, consider using an implementation that already exists, such as extending your class from FutureTask. 如果您根本没有资源为Future实现编写定时get,请考虑使用已存在的实现,例如从FutureTask扩展您的类。

Yes, you are right. 是的,你是对的。

The author of UnsupportedException is Joshua Bloch and as per his book and also from Collections design FAQ , if the object does not support an operation, the method can throw UnsupportedException. UnsupportedException的作者是Joshua Bloch,根据他的书以及Collections design FAQ ,如果对象不支持操作,该方法可以抛出UnsupportedException。

Caution should be taken before throwing this exception in your method because, it is of type RuntimeException/unchecked exception. 在将此异常抛出到方法之前应该小心,因为它的类型为RuntimeException / unchecked异常。

Link to the book 链接到这本书

Author of the book and UnsupportedException class : Joshua Bloch 本书的作者和UnsupportedException类 Joshua Bloch

/**
 * Thrown to indicate that the requested operation is not supported.<p>
 *
 * This class is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @author  Josh Bloch
 * @version %I%, %G%
 * @since   1.2
 */
public class UnsupportedOperationException extends RuntimeException {
     ...
}

When you want that the callers of your method to know that the operation is not supported you can throw the UnsupportedOperationException. 如果希望方法的调用者知道不支持该操作,则可以抛出UnsupportedOperationException。

You can check here : 你可以在这里查看

This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). 此异常扩展了RuntimeException类,因此属于在Java虚拟机(JVM)操作期间可能抛出的异常。 It is an unchecked exception and thus, it does not need to be declared in a method's or a constructor's throws clause. 它是一个未经检查的异常,因此,它不需要在方法或构造函数的throws子句中声明。 Moreover, the UnsupportedOperationException exists since the 1.2 version of Java. 此外,自1.2版本的Java以来​​,存在UnsupportedOperationException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM