什么是 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP