异常

异常的体系结构:

Throwable(最顶层)
Error:出现的不能够处理的严重问题
Exception:可以处理的问题

/*
 * 	异常的处理方式:
 * 			1.捕获处理
 * 				try...catch语句
 * 
 * 				try {
 * 					有可能出现问题的代码;
 * 				} catch(ArithmeticException ae) {
 * 					处理异常;
 * 				}
 * 
 * 				try...catch的执行顺序:
 * 					首先执行try语句
 * 						如果发现异常,异常下面的代码不在执行,直接跳入catch语句中,catch语句结束后,整个try...catch结束
 * 						如果没有发现异常,try语句执行结束后,try...catch直接结束, 不在执行catch语句
 * 
 * 
 * 			2.抛出去
 * 				当我们不想处理异常,或者没有能力处理的时候,我们可以选择抛出异常,谁调用方法谁处理异常
 * 				使用关键字throws在方法的声明出抛出异常

 * 		
 * 
 *  jvm处理异常的方式:
 *  	如果出现异常我们没有处理,jvm会帮我们进行处理,他会把异常的类型,原因还有位置显示在命令行
 *  	并且还终止了程序,异常后面的代码将不在执行
 */
public class ExceptionDemo2 {
											//IOException 是Exception 的子类,在这里的作用是一样
	public static void main(String[] args) throws Exception {
		//method();
		
		function();
		
	}
	
	public static void function() throws Exception {
		FileWriter fw = new FileWriter("a.txt");
	}

	private static void method() {
		try {
			System.out.println(1);
			System.out.println(2 / 0);
			System.out.println(2);
		} catch(ArithmeticException ae) {
			System.out.println("除数不能为0");
		}
		
		System.out.println(3);
	}

}

在这里插入图片描述

/*
 * 	如何处理多个异常:
 * 		可以使用多个try...catch语句
 * 		使用一个try和多个catch
 * 
 * 多个catch之间的顺序:
 * 			多个catch之间可以有子父类
 * 			平级之间没有顺序关系
 * 			如果有子父类,父类异常必须放在后面
 * 			
 * 	
 */
public class ExceptionDemo3 {
	public static void main(String[] args) {
		try {
			String s = null;
			System.out.println(s.length());
			
			//int[] arr = new int[5];
			//System.out.println(arr[8]);
			
			//System.out.println(2 / 0);
			
		} 
		
		catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("出现数组越界了");
		} 
		catch(NullPointerException e) {
			System.out.println("出现空指针了");
		}
		catch(Exception e) {
			System.out.println("出现异常了");
		}

/*
 * Throwable的常用方法:
		String getMessage()  
		String toString()  
		void printStackTrace()  
 	
 * 	
 */
public class ExceptionDemo4 {
	public static void main(String[] args) {
		//创建try catch的快捷键
		//选中syso,右键surround with,选择try/catch block
		try {
			System.out.println(2 / 0);
		} catch (ArithmeticException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void method() {
		try {
			System.out.println(2 / 0);
		} catch(ArithmeticException e) {
			//String getMessage() : 原因----/ by zero
			//System.out.println(e.getMessage());
			
			//String toString()  类型和原因----java.lang.ArithmeticException: / by zero
			//System.out.println(e.toString());

			//void printStackTrace():类型原因和位置----java.lang.ArithmeticException: / by zero
														at com.Demo01.Test.main(Test.java:10)
			e.printStackTrace();
		}
		//还能输出hello
		//System.out.println("hello");
	}

}

/*
 *  finally:组合try...catch使用,用于释放资源等收尾工作,无论try...catch语句如何执行,finally的代码一定会执行
 *  
 *  try {
 *  	有可能出现问题的代码;
 *  
 *  } catch(异常对象) {
 *  	处理异常;
 *  } finally {
 *  	释放资源;
 *  	清理垃圾;
 *  }
 *  
 */

public class ExceptionDemo5 {
	public static void main(String[] args) {
		//method();
		//本来在try里面的,但是finally会报错,创建对象只能在这个大括号里面,
		FileWriter fw = null;
		try {
			System.out.println(2 / 0);
			fw = new FileWriter("a.txt");
			fw.write("hello");
			fw.write("world");
			//System.out.println(2 / 0);
			fw.write("java");
			
			//fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//释放资源
			try {
				if(fw != null) {
					fw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

/*
 * 需求:写一个方法,接受考试成绩,如果考试成绩的范围在0-100之间则属于正常,否则属于异常
 * 
 * throws:处理异常的一种方式,把异常抛出,由调用者来处理
 * throw:制造异常的方式,并且结束方法
 * 
 * 注意:如果抛出(throw)的是编译时期异常,必须在方法声明处抛出(throws)
 * 
 * 如何自定义一个异常类呢?
 * 		非常简单,写一个类去继承Exception或者RuntimeException,然后实现多个构造即可
 * 
 *  */
public class ExceptionDemo7 {
	public static void main(String[] args) {
		/*boolean flag = checkScore(-10);
		System.out.println(flag);*/
		
		
		
		try {
			checkScore(110);
		} catch (Exception e) {
			//System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		
		//checkScore(110);
	}
	
/*	public static boolean checkScore(int score) {
		//判断考试成绩是否符合范围,如果不符合则返回false
		if(score < 0 || score > 100) {
			return false;
		}
	
	//符合
	return true;
	
}*/

public static void checkScore(int score) throws Exception {
	if(score < 0 || score > 100) {
		//throw new RuntimeException("考试成绩不符合要求");
		//throw new Exception("考试成绩不符合要求");
		throw new MyException("考试成绩不符合要求");
	} 
	
	System.out.println("考试成绩符合要求");
}
	
	
}
//自定义的异常类
    public class MyException extends /*RuntimeException*/ Exception{
    	//下面构造方法不用写,右键资源--superclass,只要继承父类就有
    	public MyException() {
    		super();
    		// TODO Auto-generated constructor stub
    	}

	public MyException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值