8

I have the following block of code, which uses the JSCH library found at http://www.jcraft.com/jsch/

try {
    channel.put(f, filename);
} catch (FileNotFoundException e) {
    System.out.println("no file.");
}

I know that the put method can throw a FileNotFoundException when the file specified by f is not found locally, but eclipse tells me that the catch block is unreachable, and that exception can never be thrown. When I change to:

try {
    channel.put(f, filename);
} catch (Exception e) {
    System.out.println(e.getMessage());
}

I get:

java.io.FileNotFoundException: C:\yo\hello2 (The system cannot find the file specified)

Any ideas?

4

2 に答える 2

8

FileNotFoundExceptionメソッドによってスローされた別のものにラップされているchannelため、キャッチできないと思います。

メソッドによってスローされた例外のクラスを出力してみてください。

...
} catch (Exception e) {
   System.out.println(e.getClass());
}
于 2011-09-06T16:24:54.460 に答える
2

importステートメントをチェックして、FileNotFoundException以外のパッケージからクラスをインポートしていないことを確認しますjava.io

于 2011-09-06T16:13:44.327 に答える