首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异常处理冒险

异常处理冒险
EN

Stack Overflow用户
提问于 2011-04-28 20:01:11
回答 3查看 330关注 0票数 2

我有以下代码框架

代码语言:javascript
复制
try {    
...    
...    
sysout("Please provide an input: ");
Thread.sleep(10000); //10 secs time given to the user to give the input. If the user doesn't enter the input it is throwing the exception, which is not being handled (with the custom message)
interact();  //This is a method that belongs to **expectj.Spawn** class
...
...    
} catch (Exception e) {

    System.out.println("You have not given any input!Please try again!");

}

但我仍然得到以下输出-

代码语言:javascript
复制
Exception in thread "ExpectJ Stream Piper" java.nio.channels.IllegalBlockingModeException

at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:39)
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:92)
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:86)
at java.io.InputStream.read(InputStream.java:85)
at expectj.StreamPiper.run(StreamPiper.java:134)

是否有其他类型的异常需要我处理?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-22 19:33:23

我认为扫描仪比BufferedReader更好。

代码语言:javascript
复制
ExpectJ exp = new ExpectJ(10);
String cmd = "sh test.sh";
Scanner sc = new Scanner();
Spawn s = exp.spawn(cmd);
s.expect("Name");
String answer1 = sc.next();
s.send(answer1 + "\n);
s.expect("Password");
String answer2 = sc.next();
s.send(answer2+"\n");
.
.
.
//and so on...
票数 0
EN

Stack Overflow用户

发布于 2011-04-28 20:05:33

不,IllegalBlockingModeExceptionException的子类(向下几层),所以您捕获的类型是正确的。请参阅javadoc

但是,异常可能是从另一个线程抛出的,在这种情况下,您不会在try/catch块中看到它。抛出异常的线程是"ExpectJ Stream Piper"

票数 7
EN

Stack Overflow用户

发布于 2011-05-11 16:50:08

我们可以使用bufferedreader将输入转换为一个字符串,并将该字符串传递给 exception.So (),而不是interact()。这就更方便了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5818252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档