了解UnsupportedOperationException [英] Understanding UnsupportedOperationException

查看:98
本文介绍了了解UnsupportedOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白我在哪里可以抛出这个异常。



例如,我正在实现 Future< T> 界面,不希望任何人调用该方法:



Future#get(long,TimeUnit)



所以,我可以抛出 UnsupportedOperationException

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

事情是方法的规范没有说出任何关于投掷例外。


throws表示不支持请求的操作。


Class UnsupportedOperationException我的意思是说,如果你不希望它被调用或者它被认为是错误的,那么它是很常见的,因为并不是所有的方法已实施?在我的具体情况下,我不认为调用该方法是有意义的。

技术上 UnsupportedOperationException 未被选中,因此可以抛出任何你喜欢的地方。但是将其放在意外的地方会导致您的课程不太容易使用,不推荐使用。



预期 UnsupportedOperationException 的地方被抛出的是可选操作。 Java框架包含大量的这些,特别是在Collections框架中。例如add是可选的操作,因为不可变的集合不应该允许它。抛出 UnsupportedOperationException 正是你不想写这些方法之一的。



在你的情况下,get对于使用Future而言,这是非常重要的,如果不执行,您将会产生一些惊喜。如果您要这样做,请确保它有详细的记录,并且要注意,这将导致您的Future的实现在某些情况下无法使用,并可能导致使用它的程序崩溃。



如果您根本没有资源为未来的实施编写定时获取,请考虑使用已存在的实现,例如从FutureTask扩展类。


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#get(long, TimeUnit).

So, can I just throw the 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

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. 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". The Java framework contains plenty of these, especially in the Collections framework. For example "add" is an optional operation, because immutable collections should not allow it. Throwing UnsupportedOperationException is exactly what you should do if you don't want to write one of these methods.

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. 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.

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.

这篇关于了解UnsupportedOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆