使代码与java 1.6兼容(源代码1.6中不支持使用资源进行尝试)
我有以下与Java1.7兼容的代码,但是,我需要它与Java1.6兼容。目前,我得到以下错误:try with resources在-source 1.6中不受支持 代码如下所示:使代码与java 1.6兼容(源代码1.6中不支持使用资源进行尝试),java,java-6,Java,Java 6,我有以下与Java1.7兼容的代码,但是,我需要它与Java1.6兼容。目前,我得到以下错误:try with resources在-source 1.6中不受支持 代码如下所示: try (QueryExecution qexec = QueryExecutionFactory.create(query, input.getModel())) { // Some other code while (results.hasNext()) {
try (QueryExecution qexec = QueryExecutionFactory.create(query, input.getModel())) {
// Some other code
while (results.hasNext()) {
// do something
}
return something;
}
要使其与java 1.6兼容,我需要做哪些更改?真正的答案是:
这里真正的答案是使用Java7或Java8。Java6非常古老。Java7四年前问世;Java8,差不多一年半了
只有当你有非常非常好的理由不能这样做时,才能继续阅读-
TL;博士
这个具体的例子可以简单地说:
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.
}
}
}
但这是因为这是一个非常简单的案例。例如,是否有任何其他资源需要关闭结果?或者您在处理代码本身中的一些异常,这将更加复杂
更一般的形式是
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
}
}
}
您可能需要一些实用程序库函数来帮助实现这一点,否则这将是大量的样板文件。在我知道异常已经发生的情况下,我曾经做过无声关闭的事情
详细信息:这包括在及其子章节中:
使用资源进行简单的尝试:
翻译为:
{
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();
}
}
}
}
try {
try ResourceSpecification
Block
}
[Catches]
[Finally]
您必须删除addSuppressed部分,因为Throwable在JDK6中没有该部分
使用资源进行的扩展尝试:
翻译为:
{
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();
}
}
}
}
try {
try ResourceSpecification
Block
}
[Catches]
[Finally]
…在哪里
try ResourceSpecification
Block
…被简单的资源尝试变成的大事所取代,因此整个事情变成:
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]
…这就是为什么我们如此喜欢“尝试使用资源”的说法。真正的答案是:
这里真正的答案是使用Java7或Java8。Java6非常古老。Java7四年前问世;Java8,差不多一年半了
只有当你有非常非常好的理由不能这样做时,才能继续阅读-
TL;博士
这个具体的例子可以简单地说:
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.
}
}
}
但这是因为这是一个非常简单的案例。例如,是否有任何其他资源需要关闭结果?或者您在处理代码本身中的一些异常,这将更加复杂
更一般的形式是
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
}
}
}
您可能需要一些实用程序库函数来帮助实现这一点,否则这将是大量的样板文件。在我知道异常已经发生的情况下,我曾经做过无声关闭的事情
详细信息:这包括在及其子章节中:
使用资源进行简单的尝试:
翻译为:
{
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();
}
}
}
}
try {
try ResourceSpecification
Block
}
[Catches]
[Finally]
您必须删除addSuppressed部分,因为Throwable在JDK6中没有该部分
使用资源进行的扩展尝试:
翻译为:
{
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();
}
}
}
}
try {
try ResourceSpecification
Block
}
[Catches]
[Finally]
…在哪里
try ResourceSpecification
Block
…被简单的资源尝试变成的大事所取代,因此整个事情变成:
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]
…这就是为什么我们如此喜欢“资源尝试”声明
为了使它与Java1.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的实现,并且没有相应的支持合同,那么强烈建议您升级到JavaSE8
为了使它与Java1.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 8try在最后一段代码中使用了两次,或者是拼写错误?@beta:no,这就是它的扩展方式。使用资源进行的扩展尝试将转换为外部尝试/捕获/最终尝试,并在其中使用资源进行简单尝试。@Puce:但这个问题的真正答案显而易见,因为它不存在:使用Java 7或8!:-编辑:我已经解决了关于如何在Java7之前的版本中使用资源的问题。也许应该有一个答案与此类似的参考问题,也许还有一些与上述最佳实践的链接?以更大更粗体的方式使用Java 7+font@beta:啊,是的,奇怪而破碎的层次结构是可丢弃的/Exception/RuntimeException。。。你需要根据自己的情况调整各种接球和投掷动作。或者花时间升级到Java 8:-try在最后一个代码段中使用了两次,或者它是一个输入错误?@beta:no,这就是它的扩展方式。使用资源进行的扩展尝试将转换为外部尝试/捕获/最终尝试,并在其中使用资源进行简单尝试。@Puce:但这个问题的真正答案显而易见,因为它不存在:使用Java 7或8!:-编辑:我已经解决了关于如何在Java7之前的版本中使用资源的问题。也许应该有一个推荐人
您是否提出了一个与此类似的问题,或者与上述最佳实践有一些链接?以更大更粗体的方式使用Java 7+font@beta:啊,是的,奇怪而破碎的层次结构是可丢弃的/Exception/RuntimeException。。。你需要根据自己的情况调整各种接球和投掷动作。或者花时间升级到Java 8:-旁注:请注意,Oracle Java SE 6甚至Oracle Java SE 7都达到了EoL。因此,如果您使用的是Oracle的实现,并且没有相应的支持合同,那么强烈建议升级到Java SE 8。旁注:请注意,Oracle Java SE 6甚至Oracle Java SE 7都已达到EoL。因此,如果您使用的是Oracle的实现,并且没有相应的支持合同,那么强烈建议您升级到Java SE 8。上面的代码允许从close发生异常,以完全隐藏try块中发生的异常。那不是个好主意。我看到的所有Java 7之前的处理都确保了close异常不能掩盖已经抛出的异常,因为它通常并不总是这样!更重要的一点是,收盘价很容易成为它的副产品。在任何情况下,日志记录都是最低要求,而不仅仅是默默地隐藏它。对不起,自动生成的消息。我的意思是,那些仅仅是代码的答案是不被鼓励的。请解释此代码回答问题的原因以及关键点是什么。上面的代码允许从关闭到完全隐藏try块中发生的异常。那不是个好主意。我看到的所有Java 7之前的处理都确保了close异常不能掩盖已经抛出的异常,因为它通常并不总是这样!更重要的一点是,收盘价很容易成为它的副产品。在任何情况下,日志记录都是最低要求,而不仅仅是默默地隐藏它。对不起,自动生成的消息。我的意思是,那些仅仅是代码的答案是不被鼓励的。请解释此代码为什么回答此问题以及关键点是什么。