Day18(异常)学习记录(异常体系 Throwable常用方法)

大纲:

Day18(异常)学习目标

【学习目标】理解、了解、应用、记忆

通过今天的学习,参训学员能够:(解释的时候说出二级目标的掌握程度)

  1. 【应用】异常体系 & 异常处理
  1. 【应用】能够阐述异常体系以及编译异常与运行异常区别
  2. 【应用】能够阐述JVM默认的异常处理过程
  3. 【应用】能够使用try…catch或throws处理异常
  4. 【应用】能够进行多异常的处理
  1. 【理解】Throwable常用方法&自定义异常
  1. 【应用】能够独立测试Throwable常用方法
  2. 【应用】能够独立定义一个自定义异常并使用
  1. 【理解】递归
  1. 【理解】能够阐述递归的定义
  2. 【应用】能够使用递归求5的阶乘
  3. 【应用】能够利用递归求出斐波那契数列

收获:

这一章主要讲了对异常的处理,包括try  catch 或 throws 或 自定义Throwable常用方法

一异常

1JVM默认处理方式

package com.itheima_01;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 	异常的处理方式:

 * 		
 * 
 *  jvm处理异常的方式:
 *  	如果出现异常我们没有处理,jvm会帮我们进行处理,他会把异常的类型,原因还有位置显示在命令行
 *  	并且还终止了程序,异常后面的代码将不在执行
 */
public class ExceptionDemo2 {
	public static void main(String[] args) throws Exception {
	   System.out.println(2/0);
	   System.out.println("hello");
     		
	}
	
 
}

2 try ... catch 处理方式

    package com.itheima_01;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 	异常的处理方式:
 * 			捕获处理
 * 				try...catch语句
 * 
 * 				try {
 * 					有可能出现问题的代码;
 * 				} catch(ArithmeticException ae) {
 * 					处理异常;
 * 				}
 * 
 * 				try...catch的执行顺序:
 * 					首先执行try语句
 * 						如果发现异常,异常下面的代码不在执行,直接跳入catch语句中,catch语句结束后,整个try...catch结束
 * 						如果没有发现异常,try语句执行结束后,try...catch直接结束, 不在执行catch语句
 * 
 * 
 
 * 		
 * 
 *  jvm处理异常的方式:
 *  	如果出现异常我们没有处理,jvm会帮我们进行处理,他会把异常的类型,原因还有位置显示在命令行
 *  	并且还终止了程序,异常后面的代码将不在执行
 */
public class ExceptionDemo2 {
	public static void main(String[] args) throws Exception {
		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);
		
	}
}

3 throws方式处理异常(和上面类似) 

4多异常处理

 package com.itheima_01;
/*
 * 	如何处理多个异常:
 * 		可以使用多个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("出现异常了");
		}
		/*try {
		} catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("出现数组越界了");
		}*/
		

	}

	private static void method() {
		try {
			String s = null;
			System.out.println(s.length());
		} catch(NullPointerException e) {
			System.out.println("出现空指针了");
		}
		
		try {
			int[] arr = new int[5];
			System.out.println(arr[8]);
		} catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("出现数组越界了");
		}
	}

}

二 Throwable常用方法&自定义异常

1Throwable常用方法

package com.itheima_01;
/*
 * Throwable的常用方法:
		String getMessage()  
		String toString()  
		void printStackTrace()  
 * 	
 */
public class ExceptionDemo4 {
	public static void main(String[] args) {
	
		
		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() : 原因
			//System.out.println(e.getMessage());
			
			//String toString()  类型和原因
			//System.out.println(e.toString());

			//void printStackTrace():类型原因和位置
			e.printStackTrace();
		}
		
		//System.out.println("hello");
	}
}

2finally概述和应用场景

  package com.itheima_01;
import java.io.FileWriter;
import java.io.IOException;

/*
 *  finally:组合try...catch使用,用于释放资源等收尾工作,无论try...catch语句如何执行,finally的代码一定会执行
 *  
 *  try {
 *  	有可能出现问题的代码;
 *  
 *  } catch(异常对象) {
 *  	处理异常;
 *  } finally {
 *  	释放资源;
 *  	清理垃圾;
 *  }
 *  
 */
public class ExceptionDemo5 {
	public static void main(String[] args) {
		//method();
		
		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();
			}
		}
		
	}

	private static void method() {
		try {
			System.out.println(2 / 1);
			
		} catch(ArithmeticException e) {
			System.out.println("除数不能为0");
		} finally {
			
			System.out.println("清理垃圾");
		}
	}

	

}

3自定义异常

 package com.itheima_01;
/*
 * 需求:写一个方法,接受考试成绩,如果考试成绩的范围在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("考试成绩不符合要求");
		} 
		
		System.out.println("考试成绩符合要求");
	}
	
	
}

三 递归

1递归,指在当前方法内调用自己的这种现象

public void method(){

System.out.println(“递归的演示”);

//在当前方法内调用自己

method();

}

2递归求5的阶乘

package com.itheima_01;
public class RecurrenceDemo {
	public static void main(String[] args) {
		int result = jC(5);
		System.out.println(result);//120
	}
	
	
	//求一个数的阶乘
	public static int jC(int n) {
		//必须要有出口
		if(n == 1) {
			return 1;
		}
		else {
			return n * jC(n - 1);
		}
	}
	
}

3​​​​​​​斐波纳挈数列

 package com.itheima_01;

public class RecurrenceDemo2 {
	public static void main(String[] args) {
		int result = method(20);//6765
		System.out.println(result);
	}
	
	public static int method(int n) {
		//如果是第一个月,只有一对兔子
		if(n == 1) {
			return 1;
		}
		//如果是第二个月,也只有一对兔子
		else if(n == 2) {
			return 1;
		}
		else {
			//如果不是第一个月和第二个月,则兔子的数量是前两个月之和
			return method(n - 1) + method(n - 2);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值