🔥博客主页: A_SHOWY
1.异常快速入门
异常捕获
package com.hspedu.exception_;/*
* @author SHOWY
*/
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
public class Exception01 {
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;
//int res = num1 / num2;
//当执行到num1/ num2 时,程序就会出现抛出一场Arithmet(算数)icExceptiion
//当抛出异常后,程序就推出,崩溃,下面的代码就不再执行了
//不应该出现一个不致命的问题导致系统崩溃
//java设计者提供了一个异常处理机制解决该问题
//如果程序员觉得一段代码可能出现一场,可以使用try-catch异常处理机制解决,保证程序的健壮性
//ctrl + alt + T 6
//如果引进了异常处理,即使出现了异常,程序可以继续执行
try {
int res = num1 / num2;
} catch (Exception e) {
// throw new RuntimeException(e);
System.out.println(e.getMessage());
}
System.out.println("程序继续运行");
}
}
2.异常基本概念
3.异常体系图(重点) 
这个虚线就是实现这个接口 ,体现了实现和继承的关系,这个Exception分为运行时异常和编译异常(有很多)。小结如下
补:运行时异常一般编译器检查不出来
4.常见运行时异常
4.1 NullPointerException空指针异常
当应用程序在需要的地方使用null时,抛出该异常
public class NullPointerException_ {
public static void main(String[] args) {
String name = null;
System.out.println(name.length());
}
}
4.2 ArithmeticException数学运算异常
当出现异常的运算条件时,抛出此异常,例如一个整数除以0
4.3 ArrayIndexOutOfBoundsException数组下标越界异常
public class ArrayIndexOutOfBoundsException {
public static void main(String[] args) {
int[] arr = {1,2,4};
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
}
}
4.4 ClassCastException 类型转换异常
当试图将对象强制转换为不是实例的子类时,抛出该异常
public class CastException {
public static void main(String[] args) {
A b = new B();//向上转型
B b2 = (B)b;//向下转型,OK
C c2 = (C)b;//B和C没有关系,所以抛出ClasscastException
}
}
class A{}
class B extends A{}
class C extends A{}
4.5 NumberFormatException 数字格式不正确异常
当应用程序试图将字符串转成一种数值类型,但该字符串不能转换成适当格式时,抛出该异常=》使用异常我们可以确保输入是满足条件数字
public class NumberFormatEx {
public static void main(String[] args) {
String name = "guozi";
int num = Integer.parseInt(name);//转成int
System.out.println(num);
}
}
5. 编译异常
编译异常指的是在编译期间,就必须处理的异常,否则代码不能通过编译
一般发生在网络、数据库和文件操作的时候
6 异常课堂练习
6.1
数组最大是三个元素,数组下标越界异常 ArrayIndexOutOfBoundsException
6.2
cat已经被置空了,会抛一个空指针异常 NullPointerExecption
6.3
3/0数学运算异常ArithmeticalException
6.4
会出现ClassCastException 类型转换异常:person = (Person)obj。
7.异常处理总览(重点)
try -catch -finally处理机制示意图
通常将释放资源的代码放在finally里保证一定会关闭
throws处理机制图
最高层就是JVM,它处理的机制就是直接输出异常信息并中断退出程序。
假如在抛出异常过程中没有加try -catch 也没有加throws那它默认的就是throws,向上扔的动作
7.1 try - catch 异常处理
7.1.1 细节
package com.hspedu.exception_;/*
* @author SHOWY
*/
public class trycatchdetail {
public static void main(String[] args) {
//如果try代码块可能有多个异常可以使用多个catch分别捕获不同的异常相应处理
//!要求子类异常必须写在前面,父类异常写在后
try {
Person person = new Person();
// person = null;
System.out.println(person.getName());//NullPointerException
int n1 = 10;
int n2 = 0;
int res = n1 / n2;//ArithmeticException
}
catch (NullPointerException e){
System.out.println("空指针异常" + e.getMessage());
}
catch (ArithmeticException e){
System.out.println("算数异常" + e.getMessage());
}
catch (Exception e) {
throw new RuntimeException(e);
} finally {
}
}
}
class Person{
private String name = "jack";
public String getName() {
return name;
}
}
package com.hspedu.exception_;/*
* @author SHOWY
*/
public class trycatchdetail3 {
public static void main(String[] args) {
try {
int n1 = 10;
int n2 = 0;
System.out.println(n1 / n2);
}
//finally可以抽象的认为是写遗言一样,这个程序继续执行这句话是不会执行的,因为没有catch
finally {
System.out.println("执行了finally");
}
System.out.println("程序继续执行");
}
}
7.1.2 异常处理课堂练习
***1.
这个题目很有坑!!!因为finally必须执行,所以虽然有空指针异常,但是只能返回4
2.
返回的仍然是4!还是空指针异常i++还是会执行但不返回,所以还是到finally返回4
3.
这个同样是空指针异常,用临时变量先存这个3,然后看finally。最后输出:i = 4,3
7.1.3 小结
7.1.4 课后练习
7.1.5 最佳实践 -7.1.4解题
看具体的思路在代码里,回顾一下scanner.next.重点在如果没有异常就break
package com.hspedu.exception_;/*
* @author SHOWY
*/
import java.util.Scanner;
public class trycatchexercise {
public static void main(String[] args) {
//用户输入的不是一个整数,就提示他反复输入,直到输入一个整数为止
//思路
//1.创建一个scanner对象
//2.使用无线循环接收一个输入
//3.将该输入的值转成一个int
//4如果转换的时候抛出异常说明输入的不是一个可以转成int的内容
//5.如果没有抛出异常, 则break该循环
Scanner scanner = new Scanner(System.in);
int num = 0;
String input = "";
while(true){
System.out.println("请输入一个整数");
input = scanner.next();
try {
num = Integer.parseInt(input);//这时候就有可能抛出异常
break;
} catch (NumberFormatException e) {
System.out.println("你输入的不是整数");
}
// Integer.parseInt(scanner.next());//next方法就是把你输入的当作一个字符串
}
System.out.println("你输入的值是" + num);
}
}
7.2 throws异常处理
7.2.1 基本介绍
demo:对1)的解读:2)是显而易见的
package com.hspedu.exception_;/*
* @author SHOWY
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class thows_demo {
public static void main(String[] args) {
}
public void f2() throws FileNotFoundException {
//创建一个文件流对象
//1.这里的异常是一个 FileNotFoundException 编译异常
//2.可以使用前面讲过的try -catch - finally
//3.使用throws,抛出异常,让调用f2方法的调用者处理
FileInputStream fis = new FileInputStream("D:\\java\\f\\f2.txt");
}
}
7.2.2 throws 异常处理细节
部分解释:其中还有两个细节理解,就是编译异常需要显示处理,而运行异常有默认处理机制
package com.hspedu.exception_;/*
* @author SHOWY
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class throwsdetail {
public static void main(String[] args) throws Exception {//最终就抛给了jvm
f2();
}
public static void f2() throws ArithmeticException {
//2.对于运行时异常,程序中如果没处理,默认是throws处理
int n1 = 10;
int n2 = 0;
double res = n1 / n2;
}
//补充细节
public static void f1(){
f3();//这里报错了
//原因:f3会抛出一个异常编译异常但是在f1中没有处理,这时就要求f1方法必须处理这个编译异常
}
public static void f3() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d://aa.txt");
}
//补充细节2
public static void f4() {
//1.这里在f4中调用f5是ok的
//2.原因是f5抛出的是运行异常是有默认处理的,并不要求程序员显示的处理
f5();
}
public static void f5() throws ArithmeticException {}
}
//3.子类重写父类方法的时候 对抛出异常的规定:子类重写的方法所抛出的异常类型要么和父类一致要么为父类抛出异常的子类类型
class Father{
public void method() throws RuntimeException {
}
}
class Son extends Father{
public void method() throws RuntimeException {}
// public void method() throws Exception {}报错
}
8 自定义异常
*8.1步骤
demo:
package com.hspedu.exception_.costomException;/*
* @author SHOWY
*/
public class Customexception {
public static void main(String[] args) {
int age = 6;
if(!(age >= 18 && age <= 120)){
//这里可以通过构造器设置信息
throw new AgeException("年龄需要在18-120之间");
}
System.out.println("你得年龄范围是正确的");
}
}
//自定义异常
//1.一般情况下继承RuntimeException及把自定义异常做成运行时异常,可以使用默认处理机制,编译异常就跑不了了
class AgeException extends RuntimeException {
public AgeException(String message) {//构造器
super(message);
}
}
8.2 throw 和throws 的区别
要注意先finally
9 课后作业
9.1
注:这个缺少命令行参数的意思就是越界异常
package com.hspedu.exception_.costomException;/*
* @author SHOWY
*/
public class lianxi1{
public static void main(String[] args) {
try {
if(args.length != 2){
throw new ArrayIndexOutOfBoundsException("参数个数不对");
}
//先把接受到的参数转为整数
int n1 = Integer.parseInt(args[0]);
int n2 = Integer.parseInt(args[1]);
double res = cal(n1,n2);
System.out.println("计算结果是" + res);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
} catch (NumberFormatException e) {
System.out.println("参数格式不正确,要输出整数");
} catch (ArithmeticException e) {
System.out.println("出现了除0情况");
}
}
public static double cal(int n1, int n2){
return n1/n2;
}
}
记得配置参数
9.2
9.3
BCD√ ,因为try代码块里抛出异常,后边的就不再输出
9.4
BCD,因为try-catch把这个异常处理过了不会导致程序崩溃,所以D也要输出