一、finally 的使用说明
1、finally 的理解
1、我们将一定要被执行的代码声明在 finally 结构中。 2、更深刻的理解:无论 try 中或者 catch 中是否存在未被处理的异常,无论 try 中或 catch 中是否存在 return 语句等,finally 中声明的语句都一定要被执行。 3、finally 语句和 catch 语句是可选的,但是 finally 不能单独使用。 4、try-catch 可以嵌套使用。
2、什么样的代码我们一定要声明在 finally 中呢?
我们在开发中,有一些资源,比如:输入流、输出流、数据库连接、socket 连接等资源,在使用完以后,必须显式的进行关闭操作,否则,GC 不会自动的回收这些资源。进而导致内存泄漏。 为了保证这些资源在使用完之后,不管是否出现了未被处理的异常的情况下,这些资源能被关闭。我们必须将这些操作声明在 finally 中。
package trycatchfinally;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FinallyTest {
public static void main(String[] args) {
FinallyTest f = new FinallyTest();
f.test1();
}
public void test1() {
try {
String str = "123";
str = "abc";
int i = Integer.parseInt(str);
System.out.println(i);
} catch (NumberFormatException e) {
e.printStackTrace();
System.out.println(10 / 0); // 在 catch 中抛出异常
} finally {
System.out.println("程序执行结束");
}
}
public void test2() {
FileInputStream fis = null;
try {
File file = new File("D:\\hello.txt");
fis = new FileInputStream(file); // 可能报 FileNotFoundException
int data = fis.read(); // 可能报 IOException
while (data != -1) {
System.out.println((char) data);
data = fis.read(); // 可能报 IOException
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close(); // 可能报 IOException
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、异常处理的方式二:throws
1、格式
在方法的声明处,使用 throws 异常类型 1,异常类型 2,…
2、举例
public void test() throws 异常类型 1, 异常类型 2, ... {
// 可能存在编译时异常
}
3、是否真正处理了异常?
从编译是否能通过的角度看:看成是给出了异常万一要是出现时候的解决方案。此方案就是,继续向上抛出 (throws)。 但是,此 throws 的方式,仅是将可能出现的异常抛给了此方法的调用者,此调用者仍然需要考虑如何处理相关异常。从这个角度来看,throws 的方式不算真正意义上处理了异常。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ThrowsTest {
public static void main(String[] args) {
test3();
}
public static void test3() {
try {
test2();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void test2() throws FileNotFoundException, IOException {
File file = new File("D:\\hello.txt");
FileInputStream fis = new FileInputStream(file); // 可能报 FileNotFoundException
int data = fis.read(); // 可能报 IOException
while (data != -1) {
System.out.println((char) data);
data = fis.read(); // 可能报 IOException
}
fis.close(); // 可能报 IOException
}
}
4、方法重写的要求(只针对编译型异常)
子类重写的方法抛出的异常类型可以与父类被重写的方法抛出的异常类型相同,或者是父类被重写的方法抛出异常类型的子类。
5、开发中,如何选择异常处理的两种方式?
1、如果程序代码中,涉及到资源的调用 (流、数据库连接、网络连接等),则必须考虑使用 try-catch-finally 来处理,保证不出现内存泄漏。 2、如果父类被重写的方法没有 throws 异常类型,则子类重写的方法中如果出现异常,只能考虑使用 try-catch-finally 进行处理,不能 throws。 3、开发中,方法 a 中依次调用了方法 b,c,d 等方法,方法 b,c,d 之间是递进关系。此时,如果方法 b,c,d 中有异常,我们通常选择使用 throws,而方法 a 中通常选择使用 try-catch-finally。
三、使用 throw 手动抛出异常对象
1、为什么需要手动抛出异常
在实际开发中,如果出现不满足具体场景的代码问题,我们就有必要手动抛出一个指定类型的异常对象。
2、如何理解 自动 vs 手动 抛出异常
过程 1:'抛' '自动抛':程序在执行的过程当中,一旦出现异常,就会在出现异常的代码处,自动生成对应异常类的对象,并将此对象抛出。 '手动抛':程序在执行的过程当中,不满足指定条件的情况下,我们主动的使用 "throw + 异常类的对象" 方式抛出异常对象。
过程 2:'抓' 狭义上讲:try-catch 的方式捕获异常,并处理。 广义上讲:把'抓'理解为'处理'。则此时对应着异常处理的两种方式:
- try-catch-finally
- throws
3、如何实现手动抛出异常
在方法内部,满足指定条件的情况下,使用'throw 异常类的对象'的方式抛出。
public class Throw {
public static void main(String[] args) {
Throw t = new Throw();
t.regist(-9);
System.out.println(t);
}
int id;
public void regist(int id) {
if (id > 0) {
this.id = id;
} else {
// todo 手动抛出异常
throw new RuntimeException("输入的 id 非法");
}
}
@Override
public String toString() {
return "Throw{" + "id=" + id + '}';
}
}
注意一点:throws 后的代码不能被执行,编译不通过。


