Pregunta

I know this task is supposed to mimic the structure of try/catch in traditional programming languages, but I can't for the life of me come up with a scenario where <finally> would actually be useful in an Ant script.

For example:

<trycatch>
  <try>
    <copy file="foo.txt" todir="bar" />
  </try>
  <catch>
    <echo message="could not copy" />
  </catch>
  <finally>
    <echo message="all done" />
  </finally>
</trycatch>

...would be functionally identical to:

<trycatch>
  <try>
    <copy file="foo.txt" todir="bar" />
  </try>
  <catch>
    <echo message="could not copy" />
  </catch>
</trycatch>
<echo message="all done" />

The Ant engine will always move to the next line after the trycatch block after it completes, so what's the point of including a block of code that executes at the end of it?

For reference: http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html

¿Fue útil?

Solución

In the first snippet, "all done" will be printed regardless of whether the catch block was successful.

In the second snippet, "all done" will not be printed if the catch block was unsuccessful.

Both your examples show a trivial catch block, containing a simple echo. Consider what would happen if the catch block contained a task that might actually fail...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top