我需要使用scp从远程主机复制一些文件。
如果文件不在那里,我需要显示一条消息。
我遇到的问题是,我无法捕获FileNotFound
异常。我所拥有的只是一个BuildException,但即使连接无法完成,也会引发此异常。
这是我的代码
def ant = new AntBuilder()
ant.scp(
trust:true,
file:"theFileToLook",
todir:"destinationFolder",
keyfile: "myrivateKeyFile",
verbose:true
)
当我把
catch(Exception e){
log.error("error", e)
}
我有以下内容:
java.io.IOException: scp: theFileToLook: No such file or directory
at org.apache.tools.ant.taskdefs.optional.ssh.ScpFromMessage.startRemoteCpProtocol(ScpFromMessage.java:189)
at org.apache.tools.ant.taskdefs.optional.ssh.ScpFromMessage.execute(ScpFromMessage.java:143)
at org.apache.tools.ant.taskdefs.optional.ssh.Scp.download(Scp.java:276)
at org.apache.tools.ant.taskdefs.optional.ssh.Scp.execute(Scp.java:221)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at MyService$$EODjgsH6.executeScp(MyService.groovy:98)
at MyService$$EODjgsH6.getFileFromRemoteHost(MyService.groovy:63)
at MyController.get(RetrieveController.groovy:36)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
当我放置println e.class
时,它显示BuildException
。问题是,如果我试图捕获IOException,它不会在捕获中传递。
有什么想法吗?
所以看起来Ant使用BuildException
来包装抛出的任何异常。
要显示消息,您需要实现以下内容:
def ant = new AntBuilder()
try {
ant.scp(
trust:true,
file:"theFileToLook",
todir:"destinationFolder",
keyfile: "myrivateKeyFile",
verbose:true
)
}
catch( BuildException ex ) {
if( ex.exception instanceof IOException ) {
println "Whoops! $ex.exception.message"
}
else {
throw ex
}
}