2014-05-06 30 views
0

我有這樣的情況例外:如何捕捉由於外部的JAR

我試圖執行一個命令(DF -h即命令來查找磁盤的可用空間)的數據庫服務器上。我試圖通過使用兩個JAR文件來實現,即jsch-0.1.51.jar和ganymed-ssh2-build210.jar。

現在,當有任何撥錯credentails進入,它會導致以下異常

com.jcraft.jsch.JSchException 

但是當我試圖捕獲了異常,它說:

Unreachable catch block for JSchException. This exception is never thrown from the try statement body 

和程序失敗我該怎麼趕上這個「com.jcraft.jsch.JSchException」異常。

歡迎任何建議。提前致謝。下面

完整的堆棧跟蹤給定:

Connect fails with the following exception: com.jcraft.jsch.JSchException: java.net.UnknownHostException: Select Option 

會話斷開 com.jcraft.jsch.JSchException:會話處於down

在com.jcraft.jsch.Session.openChannel(會話。 java:752) at net.neoremind.sshxcute.core.SSHExec.exec(SSHExec.java:164) at org.nrift.SchMaint.controller.CheckSpaceServlet.doPost(CheckSpaceServlet.java:63) at javax.servlet。 http.HttpServlet.service(HttpServlet.java:647) at javax.serv let.tttt.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain。 java:210)

+0

你能提供完整的堆棧跟蹤嗎? – CodeChimp

+0

儘量只捕捉一般「異常」。 – Xdg

+0

您是否閱讀過這篇文章? [這裏](http://stackoverflow.com/questions/2003419/com-jcraft-jsch-jschexception-unknownhostkey)你的問題是這樣嗎? – TeachMeJava

回答

1

那麼,從我可以告訴該方法沒有聲明拋出一個異常,所以你的IDE抱怨說,這個異常永遠不會拋出。作爲一種變通方法,您可以封裝方法調用是這樣的:

public void openChannelHelper() throws com.jcraft.jsch.JSchException { 
    // call your method here.  
} 

這應該讓你做這樣的事情:

try { 
    openChannelHelper(); 
} catch (com.jcraft.jsch.JSchException e) { 
    // handle the exception here 
} 

希望這有助於。

0

我認爲你已經寫了多個catch塊,但順序是不正確的。你應該試試波紋管

try { 
    //your code will be here where JSchException may occur 
} catch (com.jcraft.jsch.JSchException e) { 
    // write here what you want if JSchException occur 
} catch (Exception e) { 
    // this block for other exception 
}