在Java8上尝试使用resource-finally关键字错误

在Java8上尝试使用resource-finally关键字错误,java,java-8,Java,Java 8,我有下面的代码,它产生了一个语法错误,插入“Finally”来完成约会。 private synchronized void writeDurationToFile(String sFileName, Date dtStartTime, long lDuration, String sStatus) { if(!sRHTimeStamp.isEmpty()) { String sFullPath = sFileName + "_" + sRHTimeStamp +

我有下面的代码,它产生了一个语法错误,插入“Finally”来完成约会。

 private synchronized void writeDurationToFile(String sFileName, Date dtStartTime, long lDuration, String sStatus) {

    if(!sRHTimeStamp.isEmpty()) {

        String sFullPath = sFileName + "_" + sRHTimeStamp +  ".csv";

        try {

            if(!Paths.get(sFullPath).toFile().exists()) {

                try(    FileWriter fw = new FileWriter(sFullPath);
                        PrintWriter pw = new PrintWriter(fw);)  {

                    pw.println("Start\tEnd\tDuration");
                    pw.println(TimeUtils.getTimeStamp(true, dtStartTime) + "," + lDuration + "," + sStatus);
                }

            }else{

                try(FileWriter fw = new FileWriter(sFullPath, true); PrintWriter pw = new PrintWriter(fw);)  {

                    pw.println(TimeUtils.getTimeStamp(true, dtStartTime) + "," + lDuration + "," + sStatus);
                }
            }
        } //Here I get the error notification
    }
}
为什么会出现此错误,如何更好地编写此错误以消除重复

pw.println(TimeUtils.getTimeStamp(true, dtStartTime) + "," + lDuration
+ "," + sStatus);
但是仍然使用“try with resource”实现

我的代码基于


谢谢

您的较大的
try
块不是对资源的尝试,而是标准的尝试。因此,它需要添加一个
catch
finally
块。

您较大的
try
块不是对资源的尝试,而是标准的尝试。因此,它需要添加一个
catch
finally
块。

try{..}
不是
try with resource
语句。

您的
try{…}
可能有意义,因为
try with resource
将为您关闭资源,但不会处理在执行
try
块期间抛出的异常。

但请注意,经典的
try
必须使用
finally
catch
语句或两者都声明。

您的方法
writeDurationToFile()
不会抛出
IOException
,而您的代码可能会抛出它。
因此,您应该将其添加到方法声明中:

 private synchronized void writeDurationToFile(String sFileName, Date dtStartTime, long lDuration, String sStatus)  throws IOException
然后删除
try{…}
以允许客户端代码处理
IOException

或者另一种方法是通过使用
catch
完成
try
语句来捕获方法中的异常
这样,客户端代码就不需要处理
IOException

try { //classic try

      // try-with-resource
      try(FileWriter fw = new FileWriter(sFullPath);
          PrintWriter pw = new PrintWriter(fw))  {
            ...
      }
      ...
      // other try-with-resource
      try(FileWriter fw = new FileWriter(sFullPath, true);
          PrintWriter pw = new PrintWriter(fw))  {
          ...
      }
} 
// catch exceptions thrown in the block/body of the `try-with-resource` statements
catch (IOException e){
   // exception handling
}

try{..}
不是
try with resource
语句。

您的
try{…}
可能有意义,因为
try with resource
将为您关闭资源,但不会处理在执行
try
块期间抛出的异常。

但请注意,经典的
try
必须使用
finally
catch
语句或两者都声明。

您的方法
writeDurationToFile()
不会抛出
IOException
,而您的代码可能会抛出它。
因此,您应该将其添加到方法声明中:

 private synchronized void writeDurationToFile(String sFileName, Date dtStartTime, long lDuration, String sStatus)  throws IOException
然后删除
try{…}
以允许客户端代码处理
IOException

或者另一种方法是通过使用
catch
完成
try
语句来捕获方法中的异常
这样,客户端代码就不需要处理
IOException

try { //classic try

      // try-with-resource
      try(FileWriter fw = new FileWriter(sFullPath);
          PrintWriter pw = new PrintWriter(fw))  {
            ...
      }
      ...
      // other try-with-resource
      try(FileWriter fw = new FileWriter(sFullPath, true);
          PrintWriter pw = new PrintWriter(fw))  {
          ...
      }
} 
// catch exceptions thrown in the block/body of the `try-with-resource` statements
catch (IOException e){
   // exception handling
}

如果没有捕获任何异常,为什么要将代码放在try块中?
try{..}
不是try with resource语句。@Eran我尝试实现此方法,以避免最终关闭资源,并以任何方式强制关闭资源。该示例仅显示了try子句的用法。如果没有捕获任何异常,为什么要将代码放入try块?
try{..}
不是try with resource语句。@Eran我尝试实现此方法,以避免最终关闭资源,并以任何方式强制关闭资源。该示例仅显示try子句的用法!!!!我只是错过了!非常感谢。我会在10分钟内接受你的回答。当然!!!!我只是错过了!非常感谢。我会在10分钟内接受你的回答。