目录
不论执行完try,还是执行完catch,最终都会执行finally的代码
思维导图
1 简介
try{
//一切正常,执行这里
}catch(Exception e){
//如果不正常,执行这里(异常)
}
如果代码出现问题时,使用 "异常机制" 比选择结构更加方便
2 异常的继承结构
异常的基类:Throwable
● RuntimeException:运行时异常。(代码在编辑(编译阶段)时不报错,在运行的时候报错)
一个异常如果RuntimeException是它的父类,则它是运行时异常
语法上,选择性处理
● 非 RuntimeException:检查异常。(编写代码时,就提示"此代码存在异常的可能性")
在编辑时,必须处理
3 异常重点
3.1 try catch
自己(当前方法)能够处理,使用try catch
try:将可能发生异常的代码用{}包裹起来
catch:捕获特定类型的异常
如果try中的代码的确发生了异常,则程序不再执行try中异常之后的代码,直接跳到catch中执行
范例:
public class Test {
public static void main(String[] args) {
test01();
}
public static void test01(){
try {
System.out.println("正常运行");
Object ob = null;
ob.equals("");
}catch (NullPointerException e){
System.out.println("空指针异常");
}
}
}
运行结果:
3.1.2 异常对象
Exception e:其中e为异常对象
常用的有两种法
● e.printStackTrace():打印堆栈信息
public class Test {
public static void main(String[] args) {
test03();
}
public static void test03(){
try{
int[] arr = new int[3];
arr[3] = 3;
}catch (Exception e){
e.printStackTrace();
}
}
}
运行结果:
● e.getMessage:提示错误内容
public class Test {
public static void main(String[] args) {
test03();
}
public static void test03(){
try{
int[] arr = new int[3];
arr[3] = 3;
}catch (Exception e){
System.out.println("getMessage:" + e.getMessage());
}
}
}
运行结果:
3.2 throws
自己(当前方法)不能处理,上交给上级(方法调用处)处理,使用throws
public class Test {
public static void main(String[] args) throws Exception{
test04();
}
public static void test04() throws NullPointerException,ClassNotFoundException{
Object ob = new Object();
ob.equals("");
Class.forName("xxx");
}
}
3.3 finally
无论正常,还是异常,始终都会执行的代码
不论执行完try,还是执行完catch,最终都会执行finally的代码
public class Test {
public static void main(String[] args) {
test02();
}
public static void test02(){
try{
Object ob = null;
ob.equals("");
}catch(NullPointerException e){
System.out.println("空指针异常");
}finally {
System.out.println("无论正常或者是异常,始终需要执行的代码块");
}
}
运行结果:
public class Test {
public static void main(String[] args) {
test02();
}
public static void test02(){
try{
Object ob = new Object();
ob.equals("");
System.out.println("正常执行");
}catch(NullPointerException e){
System.out.println("空指针异常");
}finally {
System.out.println("无论正常或者是异常,始终需要执行的代码块");
}
}
}
运行结果:
1. 即使遇到return,也仍然会执行finally
public class Test {
public static void main(String[] args) {
test03();
}
public static int test03(){
try{
int[] arr = new int[3];
arr[3] = 3;
return 0;
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("索引越界");
return 1;
}finally {
System.out.println("finally执行");
}
}
}
运行结果:
2. 除非虚拟机关闭,才不会执行finally
public class Test {
public static void main(String[] args) {
test03();
}
public static int test03(){
try{
System.exit(0);
int[] arr = new int[3];
arr[3] = 3;
return 0;
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("索引越界");
return 1;
}finally {
System.out.println("finally执行");
}
}
}
运行结果:
3.4 throw
throw:声明异常
一般和自定义异常一起使用
jdk中自带了很多类型的异常,但如果这些内置的异常,仍然不能满足项目的需求,那么就需要创建自定义异常
如何编写自定义异常
1 创建一个类,继承于Exception,调用super("异常信息")
public class MyException extends Exception{
public MyException(String message){
super(message);
}
}
2 使用 throw 声明一个自定义异常,并且进行try catch或 throws处理
public class Test {
public static void main(String[] args) {
int age = 188;
if(age <= 0 || age > 120){
try{
throw new MyException("年龄不能小于0或者大于120");
}catch (MyException e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
}
运行结果: