Java中的catch块是什么?
catch语句涉及声明您要捕获的异常类型。如果try块中发生异常,则检查try之后的catch块。如果在catch块中列出了发生的异常类型,则将异常传递到catch块的方式与将参数传递给方法参数的方式一样。
示例
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main(String args[]) {
System.out.println("Hello");
try {
File file = new File("my_file");
FileInputStream fis = new FileInputStream(file);
} catch(Exception e) {
System.out.println("Given file path is not found");
}
}
}输出结果
Hello Given file path is not found