如何捕获由外部JAR引起的异常 [英] How to catch exception caused by external JARs

查看:151
本文介绍了如何捕获由外部JAR引起的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的情况:

我正在尝试在数据库服务器上执行命令(df -h即查找磁盘可用空间的命令).我正在尝试通过使用两个JAR文件来做到这一点,即jsch-0.1.51.jar和ganymed-ssh2-build210.jar.

I am trying to execute a command (df -h i.e command to find disk free space ) on a database server. I am trying to do so by using two JAR files i.e. jsch-0.1.51.jar and ganymed-ssh2-build210.jar .

现在,当输入任何破损的尾巴时,将导致以下异常

Now when there is any worng credentails entered , It causes the below exception

com.jcraft.jsch.JSchException

但是当我尝试捕获该异常时,它会说:

But when i try to catch that exception it says:

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

并且程序失败,如何捕获此"com.jcraft.jsch.JSchException"异常.???

and the program fails how can i catch this "com.jcraft.jsch.JSchException" exception.???

任何建议都值得欢迎.预先感谢.

Any suggestion is welcomed. Thanks in advance.

完整的堆栈跟踪如下:

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

会话关闭 com.jcraft.jsch.JSchException:会话已关闭

session is down com.jcraft.jsch.JSchException: session is down

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

at com.jcraft.jsch.Session.openChannel(Session.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.servlet.http.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)

推荐答案

好吧,据我所知,该方法未声明抛出异常,因此您的IDE抱怨从未抛出该异常.作为解决方法,您可以像这样封装方法调用:

Well, from what I can tell the method is not declared to throw an exception so your IDE is complaining that the exception is never thrown. As a workaround you can encapsulate the method call like this:

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

这应该允许您执行以下操作:

this should allow you to do something like:

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

希望这会有所帮助.

这篇关于如何捕获由外部JAR引起的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆