我正在尝试制作一个cat
克隆,并且我要求它在接收到-
时接收输入。为什么我的程序不接受自定义异常?
main();是在这里:
import java.io.*;
import java.util.*;
class cat {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
try{
filePrint(args[i]);
} catch(DashException letsTryThis){
catDash();
} catch(FileNotFoundException wrong) {
System.err.println(String.format("%s: File Not Found.", args[i]));
} catch (IOException noWords) {
System.err.println(String.format("%s: File can't be read.", args[i]));
}
}
}
}
filePrint()
只是行打印出文件中的行和catDash()
接收并打印标准输入。没什么特别的。
我想要做的是有一个自定义异常,专门捕获-
并呼吁catDash()
(上面的第一个catch块)。但是,无论如何,try/catch块总是抛出异常(上面的第二个catch块)。我的问题是,我如何才能在第二块之前抓住一个特定的原因并把它扔出去呢?
我DashException
定义它自己的文件:
import java.lang.Throwable;
public class DashException extends FileNotFoundException{
public DashException(Throwable cause){
super("-")
}
}
DashException是一个FileNotFoundException ...为什么你不只是抛出一个FileNotFoundException ??? –