1、类关系图
语法:
try{
执行过程中可能出现错误的代码
} catch(可能出现的异常类型1 ){
异常处理方案
} catch(可能出现的异常类型2 ){
异常处理方案
} catch(可能出现的异常类型3 ){
异常处理方案
} finally {
无论是否发生异常都会执行的代码
}
示例1:
public static void main(String[] args) {
try {
System.out.println(3/0);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finally");
}
}
- 结果:
示例2:
public static void main(String[] args) {
File file = new File("pom.xml");
FileReader reader = null;
try {
reader = new FileReader(file);
char[] buf = new char[128];
int len = -1;
while ((len = reader.read(buf)) != -1) {
String res = new String(buf, 0, len);
System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、Multi-catch
语法:
try{
执行过程中可能出现错误的代码
} catch(可能出现的异常类型1 |可能出现的异常类型2 |可能出现的异常类型3 …){
异常处理方案
}
注意: 异常类1 |异常类2|异常类3之间不能存在直系亲属关系
示例:
public static void main(String[] args) {
int d = new Scanner(System.in).nextInt();
try {
System.out.println(3 / d);
new File("G:/dd/aa/ab.txt").createNewFile();
} catch (ArithmeticException e) {
e.printStackTrace();
} catch (IOException e) {
// System.out.println(e.getMessage());
e.printStackTrace();
}
//可以简写为
try {
System.out.println(3 / d);
new File("G:/dd/aa/ab.txt").createNewFile();
} catch (ArithmeticException | IOException e) {
e.printStackTrace();
}
}
4、try-with-resources
语法:
Try(待释放的资源的创建语句){ //创建资源时可能会抛出异常
执行过程中可能出现错误的代码
} catch(可能出现的异常类型1 |可能出现的异常类型2 |可能出现的异常类型3 …){
异常处理方案
}
示例1: 系统会自动释放资源
public static void main(String[] args) {
File file = new File("pom.xml");
try (FileReader reader = new FileReader(file);) {
char[] buf = new char[128];
int len = -1;
while ((len = reader.read(buf)) != -1) {
String res = new String(buf, 0, len);
System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例2:
public static void main(String[] args) {
try (FileReader reader = new FileReader(new File("E:/data.txt"));
FileWriter writer = new FileWriter(new File("E:/abc.txt"));) {
char[] buf = new char[16];
int len = -1;
while ((len = reader.read(buf)) != -1) {
String str = new String(buf, 0, len);
writer.write(str);
}
} catch (Exception e) {
System.out.println("cuo le");
}
}
5、抛出异常
如果方法执行过程中抛出了非RuntimeException类型的异常时,方法声明中必须使用throws显式抛出对应的异常(否则会编译时报错),以告知调用者该方法执行过程中可能会出现异常。
示例:
public class DemoTest {
public static void fun1() {
throw new RuntimeException("runtime excetion");
}
public static void fun2(Integer param) throws FileNotFoundException {
if(param == 3) {
throw new FileNotFoundException("param == 3");
}
}
public static void main(String[] args){
try {
fun2(3);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("end");
}
}
示例1:
public static void main(String[] args) throws IOException {
File file = new File("pom.xml");
FileReader reader = new FileReader(file);
char[] buf = new char[128];
int len = -1;
while((len = reader.read(buf))!= -1){
String res = new String(buf, 0, len);
System.out.println(res);
}
reader.close();
}
示例2:
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader(new File("E:/data.txt"));
char[] buf = new char[16];
int len = -1;
while((len = reader.read(buf)) != -1) {
String str = new String(buf,0,len);
System.out.println(str);
}
reader.close();
}
示例3:
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader(new File("E:/data.txt"));
FileWriter writer = new FileWriter(new File("E:/abc.txt"));
char[] buf = new char[16];
int len = -1;
while ((len = reader.read(buf)) != -1) {
String str = new String(buf, 0, len);
writer.write(str);
}
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}