2011-04-28 51 views
2

我有以下的代码框架异常处理冒险

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) 

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

+3

您确定异常正在引发您捕捉的同一线程上吗? – forsvarir 2011-04-28 12:04:24

+0

我这么认为。但我不确定。 – hari 2011-04-29 09:49:39

回答

0

我认为扫描仪比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... 
7

不,IllegalBlockingModeExceptionException(几个级别下)的子类,所以你正在捕捉正确的类型。请参阅javadoc

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

+0

但是,为什么Stream Piper会抛出一个异常?因为当我调用interact()时,我能够成功地与系统交互。它只会在调用该方法后不进行交互时在运行时引发这样的异常。好!如果所有异常都是从不同的线程抛出,我的方法是什么?我从来没有遇到过这种情况。 – hari 2011-04-29 08:49:39

1

有没有运气试图摆脱,而不是相互作用的exception.So的,()我们可以使用的BufferedReader采取输入一个字符串,该字符串传递到的send()。这样更方便。

+0

你有可能提供示例代码吗?我面临同样的问题,StreamPiper在bytes_read = inputStream.read(buffer)上失败; – Martin 2011-08-19 14:20:07