大纲:
Day18(异常)学习目标
【学习目标】理解、了解、应用、记忆
通过今天的学习,参训学员能够:(解释的时候说出二级目标的掌握程度)
- 【应用】异常体系 & 异常处理
- 【应用】能够阐述异常体系以及编译异常与运行异常区别
- 【应用】能够阐述JVM默认的异常处理过程
- 【应用】能够使用try…catch或throws处理异常
- 【应用】能够进行多异常的处理
- 【理解】Throwable常用方法&自定义异常
- 【应用】能够独立测试Throwable常用方法
- 【应用】能够独立定义一个自定义异常并使用
- 【理解】递归
- 【理解】能够阐述递归的定义
- 【应用】能够使用递归求5的阶乘
- 【应用】能够利用递归求出斐波那契数列
收获:
这一章主要讲了对异常的处理,包括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);
}
}
}