11.异常相关

异常类和内部类

1 异常类

1.1 异常类概述

image-20201028123233226

1.2 异常处理

​ 计算机语言进行项目开发的过程中,即使程序员把代码写的尽善尽美,在系统的运行过程中仍然会遇到一些问题,因为很多问题不是依靠代码能够避免的,比如:网络阻塞,客户输入数据格式不正确等。

1.2.1 异常的分类

异常:在程序执行中发生的不正常的情况称之为“异常”。顶级父类为java.lang.Throwable

  • Error:Java虚拟机无法解决的问题。如:JVM系统内部错误,资源耗尽等严重情况。比如:StackOverflowError(栈溢出)和OOM(OutOfMemoryError:堆空间溢出),一般不编写代码进行处理。

    ==>栈溢出:无限递归。

    ==>堆空间溢出:创建对象过多(创建多维数组)。

  • Exception:其他因为编程错误或偶然的外在因素导致的一般性问题,可以使用针对性代码进行处理。

    例如:空指针,数组角标越界,试图读取不存在的文件,网络连接中断。

    ①编译时异常:例如:IOException,FileNotFoundException,ClassNotFoundException.

    ②运行时异常(RuntimeException):例如:NullPointerException,ClassCastException,NumberFormatException(包装类),InputMismatchException,ArithmaticException,ArrayIndexOutOfBoundsException

1.2.2 异常的处理–抓抛模型

​ 过程一:“抛”一旦出现异常,就会在异常代码出生成一个对应异常类的对象并将此对象抛出,一旦抛出对象以后,其后的代码就不再执行。

​ 关于异常对象的产生:①系统自动生成异常对象。

​ ②手动的生成一个异常对象,并抛出异常

​ ==>throw new RuntimeException(“你输入数据非法!”)==运行时异常。

​ ==>throw new Exception(“你输入的数据非法!”)==编译时异常。

过程二:“抓”

1)try-catch-finally:捕捉可能发生异常的异常。
	
	try{
		//可能出现异常的代码
	}catch(异常类型1 变量名1){
		//处理异常1
	}catch(异常类型2 变量名2){
		//处理异常2
	}catch(异常类型3 变量名3){
		//处理异常3
	}finally{
		//一定会执行的代码
	}

说明:

  • finally是可选的。
  • 使用try对可能出现异常代码包裹起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,接着会根据此对象的类型,去catch中进行匹配。一旦try中的异常对象匹配到某一个catch时,就会进入到catch中进行异常处理,一旦处理完成就会跳出catch,继续向下执行。
  • catch中异常类型如果没有子父类关系,则谁声明在上,谁声明在下无所谓。存在父类,父类一定要在子类的后面,否则会报错==>不可达。
  • 常见的异常对象处理方式:①e.getMassage()②e.printStackTrace()
  • finally{ }中的代码一定会执行的意思:即使catch中再次出现异常,try或者catch中出现return,finally中的代码依旧会被执行==>return之前执行。
  • JVM的垃圾回收机制对于物理连接无能为力,需要我们手动的进行资源的释放==>一般放入finally进行资源的关闭。例如:数据库连接,输入输出流,网络编程中的Socket等。
  • try-catch操作可以进行嵌套。
  • 对于运行时异常,我们一般不使用try-catch来进行处理;对于编译时异常一定要进行try-catch或者throws来进行处理==>相当于将编译时异常推迟成运行时异常。
//常见的运行时异常
//NullPointerException
@Test
public void test1() {
	try {
		int[] arrays=null;
		System.out.println(arrays[0]);
		System.out.println("执行到了1");  //没有执行该语句,出现异常之后直接进入到了catch中。
	} catch (NullPointerException e) {
		System.out.println("空指针异常了");
		System.out.println(e.getMessage());
		e.printStackTrace();
	}
	System.out.println("执行到了2");	
}

//ArrayIndexOutOfBoundsException
@Test
public void test2() {
	try {
		int[] arrays=new int[10];
		System.out.println(arrays[-1]);
	} catch (ArrayIndexOutOfBoundsException e) {
		System.out.println(e.getMessage());
	}
	try {
		String string="abc";
		System.out.println(string.charAt(3));
	} catch (StringIndexOutOfBoundsException e) {
		System.out.println(e.getMessage());
	}		
}

//ClassCastException
@Test
public void test3() {
	try {
		Object o1=new Date();
		String string=(String)o1;
	} catch (ClassCastException e) {
		System.out.println(e.getMessage());
	}	
}

//NumberFormatException
@Test
public void test4() {
	try {
		String string="abc";
		int parseInt = Integer.parseInt(string);
		System.out.println(parseInt);
	} catch (NumberFormatException e) {
		System.out.println(e.getMessage());
	}
		
}
//InputMismatchException:输入不匹配
@Test
public void test5() {
	try {
		Scanner scanner = new Scanner(System.in);
		int nextInt = scanner.nextInt();
		System.out.println(nextInt);
	} catch (InputMismatchException e) {
		System.out.println(e.getMessage());
	}
}

//ArithmaticException:算数异常
@Test
public void test6() {
	try {
		int a=10;
		int b=0;
		System.out.println(a/b);  //ArithmaticException:by zero
	} catch (ArithmeticException e) {
		System.out.println(e.getMessage());
	}
	
}
//**************************************************
//编译时异常:编译的时候就报错了,需要对其进行处理:try...catch或者throws抛出异常。
@Test
public void test7(){
	FileInputStream fis = null;
	try {
		//FileNotFoundException
		fis=new FileInputStream(new File("hello.txt"));
		//IOException
		int data=fis.read();
		while(data!=-1) {
			System.out.println((char)data);
			data=fis.read();
		}
	} catch (FileNotFoundException e) {
		System.out.println(e.getMessage());
	}catch (IOException e) {
		System.out.println(e.getMessage());
	}finally {
		try {
			if(fis!=null) {
				fis.close();  //避免空指针异常
			}
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
}
//****************************************
//finally的测试
@Test
public void methodTest() {
	int method = method();
	System.out.println(method);
}
public int method() {
	try {
		int[] arrays=new int[10];
		System.out.println(arrays[10]);
		return 1;
	} catch (ArrayIndexOutOfBoundsException e) {
		e.printStackTrace();
		return 2;
	}finally {
		System.out.println("一定会执行的代码!!!");
		return 3;
		
	}
}
1.2.3 异常的处理–throws声明异常

​ throws+异常类型:抛出异常,自己不进行处理,让上级进行处理,此时所有调用该方法的方法都需要抛出该异常,直到main方法的时候,我们最好解决掉==>try-catch。

  • 一般对编译时异常进行处理。
  • 声明在方法的声明处。声明此方法在执行的过程中可能抛出的异常,一旦方体执行时,出现异常,会生成一个异常类对象和throws的异常类型进行匹配,匹配成功则向上抛出,异常代码后续的代码就不再执行。
1.2.4 开发之中如何选择使用try-catch还是throws。
  • 子类重写的规则之一:子类重写的方法的异常不大于父类被重写的方法抛出的异常类型==>开发中父类中被重写的方法没有抛出异常,子类重写的方法不能抛出异常,如果子类重写的方法需要使用try-catch来进行处理。
  • 当执行的方法A中,调用了另外几个方法且方法之间是递进的关系,推荐对这几个方法使用throws来处理,在方法A中进行统一的try-catch。

1.3 自定义异常类

1)继承于现有的异常结构:RuntimeException(uncheck非受检异常),Exception

2)提供全局常量:serialVersionUID==>唯一标识,在序列化的时候有用。

3)提供重载的构造器

4)自己抛出异常对象(抛出的异常对象之后不能添加代码),否则会报出不可达。

//自定义运行时异常
public class MyException extends RuntimeException{
	static final long serialVersionUID = -7034897197745766949L;
	public MyException() {
	}
	public MyException(String msg) {
		super(msg);
	}
}

public class MyExceptionTest {
	public static void main(String[] args) {
		Student student = new Student();
		student.regist(-10);
	}
}
class Student{
	private int id;
	public void regist(int id) {
		if (id>0) {
			this.id=id;
		}else {
            //抛出异常对象
			throw new MyException("输入的id不能是负数");
		}	
	}
}

1.4 简单总结

1 throw和throws的区别

1)相同之处:

  • 都是异常处理的关键字。
  • throws:用在方法声明上,用于声明当前方法可能产生的异常对象的类,一旦产生指定的异常,异常抛给方法的调用者。
  • throw:用在方法体中,表示产生一个指定的异常情况,肯定会产生异常对象。

2)不同:

  • 位置不同:throws用在方法的声明上,throw用在方法体中。
  • 内容不同:throws+异常类型;throw+异常对象。
  • 作用不同:throws用于声明可能产生哪些异常,一旦产生抛给调用者。throw表示:方法中产生了一个指定类型的异常对象。

1.5 练习

1.编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除。

提示:

​ 对 数 据 类 型 不 一 致 (NumberFormatException) 、 缺 少 命 令 行 参 数(ArrayIndexOutOfBoundsException、
除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
​ (1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能。
​ (2)在main()方法中使用异常处理语句进行异常处理。
​ (3)在程序中,自定义对应输入负数的异常类(EcDef)。
​ (4)运行时接受参数 java EcmDef 20 10 //args[0]=“20” args[1]=“10”
​ (5)Interger类的static方法parseInt(String s)将s转换成对应的int值。
​ 如:int a=Interger.parseInt(“314”); //a=314;

public static void main(String[] args) {
		try {
			int int1=Integer.parseInt(args[0]);
			int int2=Integer.parseInt(args[1]);
			EcmDef ecmDef = new EcmDef();
			int ecm = ecmDef.ecm(int1, int2);
			System.out.println(ecm);
		} catch (NumberFormatException e) {
			System.out.println("数据类型不一致");
		}  catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("缺少命令行参数");
		} catch (ArithmeticException e) {
			System.out.println("除0异常");
		}catch (EcDef e) {
			System.out.println(e.getMessage());
		}
	}
public int ecm(int i,int j) throws EcDef {
	if (i<0||j<0) {
		throw new EcDef("输入的参数不能是负数!!!");
	}
	return i/j;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值