Java异常

什么是异常?
	>Java代码在运行期间发生的问题就称之为异常
	
Throwable :Java 语言中所有错误或异常的超类
	| - Exception: 轻微错误--相当于人的感冒小病,可以通过程序处理还可以正常运行
		| - RuntimeException:运行时异常,在程序运行期间产生的异常
	| - Error:严重错误--相当于人得了癌症,艾滋病
	
	举例:下标越界异常处理流程
		public static void main(String[] args) {
			int[] arr = { 1, 2, 3 };
			getArray(arr);
		}
	
		public static int getArray(int[] arr) {
			int i = arr[3];
			return i + 1;
		}
		
	1.JVM检测到没有索引arr[3],数组越界了(提前预置的错误)
	2.创建异常对象
	3.将异常对象进行抛出,默认抛给调用者(main)
	4.调用者接受到异常,没有异常处理,继续抛出调用者JVM
	5.JVM收到异常
	6.JVM将异常信息以红色字体打印在控制台
	7.JVM将程序停止

throw,throws关键字


	public static void main(String[] args) throws Exception {
		int[] arr = {};
		/*
		 * 调用getArray(T[] t)方法
		 * 		数组为null或者长度为0抛出异常
		 * 举个栗子:教练让你跑步,但是你肚子疼,你不说教练也不知道所以需要告诉教练
		 * 		       声明 throws 方法内抛出的异常类或者父类
		 * 			当该方法使用throws关键字后,其调用者(教练)就知道哪里不舒服了
		 * 			具体是继续跑步还是休息看调用者(main)处理,如果main收到异常
		 * 			也进行throws那么异常会抛给虚拟机,其步骤就是将异常对象红色字体显示
		 * 			在控制台(console)然后停止程序
		 */
		getArray(arr);
	}

	public static int getArray(int[] arr) throws Exception {

		if (arr == null) {
			throw new Exception("数组为NULL");
		}
		if (arr.length == 0) {
			throw new Exception("数组没有内容");
		}
		int i = arr[3];
		return i + 1;
	}

try…catch…finally关键字
如果调用者(mian)不进行throws那么异常需要捕获处理
也可以多个catch用来捕获多异常
try…catch…catch…finally
catch的异常类型顺序,如果是多个异常,没有继承关系没有顺序,如果有则先写子类最后写父类捕获:
catch(RuntimeException){}catch(Exception e){}
鼠标放到要捕获的异常代码选择try…catch即可快速捕获,也可以选择throws或者Ctrl+1


 	public static void main(String[] args) {
		int[] arr = null;
		try {
			getArray(arr);// 要处理的异常
		} catch (Exception e) {// 捕获异常类型
			e.printStackTrace();// 如果发现异常怎么处理这个异常,可以对异常进行判断,循环等其他操作
		} finally {
			System.out.println("不管是否发生异常必须执行的代码");
		}
		System.out.println("只要代码try..catch 后面的代码会继续执行");
	}

	public static int getArray(int[] arr) throws Exception {

		if (arr == null) {
			throw new Exception("数组为NULL");
		}
		if (arr.length == 0) {
			throw new Exception("数组没有内容");
		}
		int i = arr[3];
		return i + 1;
	}


	RuntimeException:
		>方法抛出不需要throws
		>也不需要调用者捕获
		>运行异常,不能发生,如果发生需要程序员修改代码,后续代码不在执行
		>空指针,数组越界都是运行时异常,不需要处理,直接改代码


	子类重写父类抛异常的方法,子类可以不用抛异常,或者抛出父类一样异或其子类异常,不能大于父类的异常
	父类不抛异常则子类也不可以抛出异常

Throwable常用方法


	public static void main(String[] args) {
		try {
			throw new Exception("异常信息");
		} catch (Exception e) {
			System.out.println("简短异常信息:" + e.getMessage());
			System.out.println("带类名的异常信息:" + e.toString());
			System.out.println("超详细异常信息" + e.getStackTrace());// 抛出的异常不处理JVM会默认调用这个方法来显示异常信息
		}
	}


	自定义异常
	1.继承RuntimeException或其他异常,一般不会发生异常所以使用RuntimeException
	2.创建有无参构造,有残构造使用super(Message),将异常信息传递给父类进行输出控制台即可




	public class Demo2 {
	
		public static void main(String[] args) {
			int message = getMessage(-12);
			System.out.println(message);
		}
	
		public static int getMessage(int a) {
			if (a < 0) {
				throw new WrongfulException("参数不合法");// 所有Exception子类命名都是以Exception结尾
			}
	
			return a * 2;
		}
	
	}
	
	class WrongfulException extends RuntimeException {
		public WrongfulException(String s) {
			super(s);
		}
	
		public WrongfulException() {
	
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值