异常处理冒险

har*_*ari 2 java exception-handling try-catch

我有以下代码框架

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!");

}
Run Code Online (Sandbox Code Playgroud)

但我仍然得到以下输出 -

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)
Run Code Online (Sandbox Code Playgroud)

我需要处理其他任何异常吗?

Sim*_*son 7

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

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