我有以下与java 1.7兼容的代码,但是,我需要它与java 1.6兼容。目前,此代码出现以下错误:try-with-resources is not supported in -source 1.6
代码如下所示:
try (QueryExecution qexec = QueryExecutionFactory.create(query, input.getModel())) {
// Some other code
while (results.hasNext()) {
// do something
}
return something;
}
我需要更改什么才能使其适用于java 1.6?
答案 0 :(得分:4)
真正的答案:
这里真正的答案是使用Java 7或8 。 Java 6很老了。 Java 7出现四年之前; Java 8,差不多一年半了。
只有在非常非常好的原因导致您不能这样做时,才能继续阅读。 : - )
<强> TL; DR 强>
具体 示例可以简单地说:
QueryExecution qexec = QueryExecutionFactory.create(query, input.getModel());
Throwable thrown = null;
try {
// Some other code
while (results.hasNext()) {
// do something
}
return something;
}
catch (Throwable t) {
thrown = t; // Remember we're handling an exception
throw t;
}
finally {
try {
qexec.close();
}
catch (Throwable t) {
if (thrown == null) {
// Not handling an exception, we can rethrow
throw t;
}
else {
// Log it or something, you can't allow it to
// throw because there's *already* an exception
// being thrown and you'll hide it. This is why
// Java 7 added Throwable#addSuppressed.
}
}
}
但那是因为这是一个非常简单的案例。如果还有其他资源需要关闭(例如results
?)或者你在代码本身处理一些异常,那就更复杂了。
更通用的形式是;
SomeResource r1 = null;
Throwable thrown = null;
try {
r1 = new SomeResource();
SomeOtherResource r2 = null;
try {
r2 = new SomeOtherResource();
// use them
return something;
}
catch (Throwable t) {
thrown = t; // Remember we're handling an exception
throw t;
}
finally {
try {
r2.close();
}
catch (Throwable t) {
if (thrown == null) {
// Not handling an exception, we can rethrow
throw t;
}
else {
// Log it or something, you can't allow it to
// throw because there's *already* an exception
// being thrown and you'll hide it. This is why
// Java 7 added Throwable#addSuppressed.
}
}
}
}
catch (Throwable t) {
thrown = t; // Remember we're handling an exception
throw t;
}
finally {
try {
r1.close();
}
catch (Throwable t) {
if (thrown == null) {
// Not handling an exception, we can rethrow
throw t;
}
else {
// Log it or something
}
}
}
你可能想要一些实用程序库函数来帮助解决这个问题,否则它是一个很多的样板文件。在过去我知道异常已经发生的情况下,我曾经做过“沉默”的事情。
详情:§14.20.3 of the JLS及其小节涵盖了这一点:
一个简单的try-with-resources
:
try ({VariableModifier} R Identifier = Expression ...)
Block
转换为:
{
final {VariableModifierNoFinal} R Identifier = Expression;
Throwable #primaryExc = null;
try ResourceSpecification_tail
Block
catch (Throwable #t) {
#primaryExc = #t;
throw #t;
} finally {
if (Identifier != null) {
if (#primaryExc != null) {
try {
Identifier.close();
} catch (Throwable #suppressedExc) {
#primaryExc.addSuppressed(#suppressedExc);
}
} else {
Identifier.close();
}
}
}
}
您必须删除addSuppressed
部分,因为Throwable
在JDK6中没有。{/ p>
扩展try-with-resources
:
try ResourceSpecification
Block
[Catches]
[Finally]
转换为:
try {
try ResourceSpecification
Block
}
[Catches]
[Finally]
......那里
try ResourceSpecification
Block
...被简单try-with-resources
变成的大事所取代,所以整个事情变成了:
try {
{
final {VariableModifierNoFinal} R Identifier = Expression;
Throwable #primaryExc = null;
try ResourceSpecification_tail
Block
catch (Throwable #t) {
#primaryExc = #t;
throw #t;
} finally {
if (Identifier != null) {
if (#primaryExc != null) {
try {
Identifier.close();
} catch (Throwable #suppressedExc) {
#primaryExc.addSuppressed(#suppressedExc);
}
} else {
Identifier.close();
}
}
}
}
}
[Catches]
[Finally]
...这就是我们非常喜欢try-with-resources
陈述的原因。
答案 1 :(得分:0)
我需要更改什么才能使其适用于java 1.6?
请尝试以下代码:
QueryExecution qexec = null;
try {
qexec = QueryExecutionFactory.create(query, input.getModel()));
// Some other code
while (results.hasNext()) {
// do something
}
return something;
} finally {
if (qexec != null){
qexec.close();
}
}
最重要的部分:必须在finally块中手动关闭流。
请注意,Oracle Java SE 6现在甚至Oracle Java SE 7都已达到EoL。因此,如果您正在使用Oracle的实施并且没有相应的支持合同,那么强烈建议您升级到Java SE 8