我有以下代码框架
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!");
}但我仍然得到以下输出-
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)是否有其他类型的异常需要我处理?
发布于 2011-08-22 19:33:23
我认为扫描仪比BufferedReader更好。
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...发布于 2011-04-28 20:05:33
不,IllegalBlockingModeException是Exception的子类(向下几层),所以您捕获的类型是正确的。请参阅javadoc。
但是,异常可能是从另一个线程抛出的,在这种情况下,您不会在try/catch块中看到它。抛出异常的线程是"ExpectJ Stream Piper"。
发布于 2011-05-11 16:50:08
我们可以使用bufferedreader将输入转换为一个字符串,并将该字符串传递给 exception.So (),而不是interact()。这就更方便了。
https://stackoverflow.com/questions/5818252
复制相似问题