2014-03-01 26 views
0

我知道這個任務應該模仿傳統編程語言中try/catch的結構,但是我不能在我的生活中想出一個場景,其中<finally>實際上在Ant腳本中很有用。ant-contrib的trycatch中「finally」元素的用途是什麼?

例如:

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

...在功能上等同於:

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

螞蟻引擎將會移動到下一行的trycatch塊後完成後,有啥包括在其結尾處執行的代碼塊的要點?

參考:http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html

回答

0

在第一片段中,「全部完成」將不管catch塊是否成功印刷。

在第二個片段中,如果catch塊不成功,則會打印「全部完成」而不是

這兩個例子都顯示了一個簡單的catch塊,其中包含一個簡單的echo。考慮如果catch塊包含可能實際上失敗的任務會發生什麼......

相關問題