Exception

1异常的分类

1异常的概念

异常:异常是指在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序。简单来说    就是程序出现 了不正常的情况。

异常本质就是Java当中对可能出现的问题进行描述的一种对象体现

2异常的分类

编译时异常: 在程序执行出现的异常错误,这是可以解决的,在程序运行前处理 
运行时异常: 在程序执行的过程中出现的问题,这个也是可以解决,检查代码的逻辑即可
严重错误: 这是无法处理,叫做错误(Error),这个我们不学习如何处理

Exception
编译时异常(受检异常): 不是RuntimeException的子类但是是Exception的子类我们称为编译时异常
运行时异常(非受检异常): RuntimeException或者RuntimeException的子类称为运行异常
Error

2 异常的处理

1 为什么需要处理异常?

  就算程序出现问题也要能够让程序继续执行
  处理异常的本质是?
-- 就算程序出现问题也要能够让程序继续执行

2 JVM默认处理异常的方式?

1.打印错误信息
	a.异常名称 (异常全路径)
	b.异常的消息
	c.异常所在的方法
	d.异常的行号
	
2.将程序终止  System.exit(0);

3  JVM处理异常的方式不能够满足我们的需求,所以我们需要自己来处理异常
          如何来处理异常?
          处理异常的格式:
          
          方式一: try...catch...finally
          方式二: throws

3 处理异常 方式一

	try {
	// 放置程序可能出现问题的代码
     } catch(异常类型 异常名){
    	// 异常处理的代码
     } catch(异常类型 异常名){
	// 异常处理的代码
     } ... finally {
   	// 释放资源
    }

4 处理异常 方式二 throws

1 为什么有了try…catch还需要throws处理?

  1.第一个我没有能力处理
  2.我没有权限处理
  3.压根就不想处理
  就可以使用throws关键字抛出,抛给调用者处理,谁调用谁处理

2 throws异常处理的格式:

throws使用格式
[修饰符] 返回值类型 方法名(参数列表) [throws 异常类1,异常类2....]{
}

3 注意:

	1.在主方法中,不能够throws抛出,因为main方法是虚拟机调用,虚拟机处理异常是将程序终止,不满足我们处理异常的本质
	2.当一个方法抛出的是编译时异常,调用者必须处理。
	3.throws可以声明多个异常
	4.当一个父类方法抛出一个异常,重写方法不能够被扩大
	5.throws表示异常出现的一种可能性
	6.多个平级异常的处理方式: |隔开多个异常
public class ExceptionDemo06 {
	public static void main(String[] args)  {
	// 方式一
		Zi zi = new Zi();
		try {
			zi.show();
		} catch (NullPointerException|ClassCastException|ArithmeticException e) {
			// TODO: handle exception
		}catch (Exception e) {
		}	
	}
	//方式二
	public static void method()   throws ArithmeticException, ArrayIndexOutOfBoundsException{
		System.out.println("Start");
		int a = 10;
		int b = 0;
//		System.out.println(a/b);
		int[] arr = new int[3];
		System.out.println(arr[3]);
		System.out.println("End");
	}
	public static void method2() throws ParseException {
		String s = "1995-02-05 12:21:23";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm+ss");
		Date d = sdf.parse(s);
		System.out.println(d);
	}
public static void show() throws ParseException {
		method2();
	}	
}
class Fu {
	public void show() throws Exception {		
	}
}
class Zi extends Fu {
	@Override
	public void show() throws NullPointerException,ArithmeticException,ClassCastException {
	}
}

5 throws和throw区别

1.throws出现方法的声明上,throw出现在方法体内
2.throws表示异常出现的一种可能性,throw表示方法一定出现了异常
3.throws可以声明很多个异常,throw只能够抛出一个异常对象
4.throws声明的是异常类,throw抛出的是异常对象

public class ExceptionDemo07 {
	public static void main(String[] args) {
		try {
			method();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String s = null;
		try {
			s = new String(new byte[] {97, 98, 99, 100} , -1, 2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Over");	
	}
public static void method() throws ArithmeticException {
		int a = 10;
		int b = 0;
//		 方式一: if处理
		/*if (b != 0) {
			System.out.println(a / b);
		}*/
		// 方式二: 异常处理
		if (b == 0) {
			// ArithmeticException ae = new ArithmeticException("by /zero");
			throw new ArithmeticException("by /zero");
		}
		System.out.println(a / b);		
	}
		public static void method2() throws ParseException {
		String s = "1995-02-05 12:21:23";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm+ss");
		Date d = null;
		try {
			d = sdf.parse(s);
		} catch (ParseException e) {
//			e.printStackTrace();
//			throw 自定义异常;
		}
		System.out.println(d);
	}
}

6 注意:

1.try块不能够独立存在
2.一旦try块出现异常,那么try块下面无论有多少行代码都不会被执行
4.try块代码越少越好
5.catch的格式从上到下是先子类后父类

7 异常的执行流程:

    1.异常执行到异常处, 系统会抛出一个异常对象  
	ArithmeticException e = new ArithmeticException("/ by zero");
	throw e;
	2.程序会将异常对象和 catch的形参类型进行逐个匹配
	3.匹配成功执行catch块,程序继续执行
	4.匹配失败交给虚拟机处理

8 标准的异常处理格式:

    1.能够显示处理的异常尽量显示声明,提高程序的可读性
	2.就算你全部都声明了还需要在异常的最后面添加 exception捕获异常,提高了程序的安全性
public class ExceptionDemo02 {
	public static void main(String[] args) {
		System.out.println("Start");
		int a = 10;
		int b = 0;
		int[] arr = new int[3];
		Object obj = null;
		Object o = new Integer(100);
		
		
		try {
//			System.out.println(a / b);
//			System.out.println(arr[3]); // throw new ArrayIndexOutOfBoundsException("...");
//			obj.equals("1313"); // throw new ClassCastException("...");
			String s = (String)o;
		} catch (ArithmeticException ae) {
			System.out.println("除数不能为0!!!");
		} catch (ArrayIndexOutOfBoundsException aiobe) {
			System.out.println("数组越界");
		} catch (NullPointerException aiobe) {
			System.out.println("空指针异常");
		} catch (Exception e) { // Exception e = new ClassCastException("...");
			System.out.println("出问题了");
		} 
		
		System.out.println("Over");
	}
}

3 Throwable类

1.Throwable作为所有异常和错误的父类
2.只有作为Throwable或者它的子类对象才能够Java虚拟机抛出,或者可以由Java throw语句抛出
3.只有这个类或其子类可以是catch子句中的参数类型
4.异常的本质就是对错误信息的一种对象体现

4 finally关键字

1  用于try块成员资源的释放
   finally无论什么情况都会被执行,除非遇到系统退出的语句
   finally和return?

2  finally碰到return
    finally一定会执行

执行顺序?
在try语句中,在执行return语句时,要返回的结果已经准备好了,就在此时,程序转到finally执行了。
在转去之前,try中先把要返回的结果存放到不同于x的局部变量中去,执行完finally之后,在从中取出返回结果,
因此,即使finally中对变量x进行了改变,但是不会影响返回结果。它应该使用栈保存返回值。

final,finally,finalize的区别
public class ExceptionDemo08 {
	public static void main(String[] args) {
//		String s = null;
//		try {
//			s = new String(new byte[] {97, 98, 99, 100} , -1, 2);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		System.out.println(s.length());
//		System.out.println("Over");	
		try {
			method();
//			return;			
		} catch (ArithmeticException e) {
			e.printStackTrace();
//			return;
//			System.exit(0);
			Runtime.getRuntime().exit(0);
		} finally {
			System.out.println("我被执行了!!!");
		}
		System.out.println("Over");		
	}
		public static void method() throws ArithmeticException {
		int a = 10;
		int b = 0;

		if (b == 0) {
			throw new ArithmeticException("by /zero");
		}
		System.out.println(a / b);		
	}
}

4 finally关键字

1  用于try块成员资源的释放
   finally无论什么情况都会被执行,除非遇到系统退出的语句
   finally和return?

2  finally碰到return
    finally一定会执行

执行顺序?
在try语句中,在执行return语句时,要返回的结果已经准备好了,就在此时,程序转到finally执行了。
在转去之前,try中先把要返回的结果存放到不同于x的局部变量中去,执行完finally之后,在从中取出返回结果,
因此,即使finally中对变量x进行了改变,但是不会影响返回结果。它应该使用栈保存返回值。

final,finally,finalize的区别
public class ExceptionDemo08 {
	public static void main(String[] args) {
//		String s = null;
//		try {
//			s = new String(new byte[] {97, 98, 99, 100} , -1, 2);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		System.out.println(s.length());
//		System.out.println("Over");	
		try {
			method();
//			return;			
		} catch (ArithmeticException e) {
			e.printStackTrace();
//			return;
//			System.exit(0);
			Runtime.getRuntime().exit(0);
		} finally {
			System.out.println("我被执行了!!!");
		}
		System.out.println("Over");		
	}
		public static void method() throws ArithmeticException {
		int a = 10;
		int b = 0;

		if (b == 0) {
			throw new ArithmeticException("by /zero");
		}
		System.out.println(a / b);		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值