JAVA自学教程之异常及其思想 (一)



JAVA自学教程之异常及其思想 (一)。异常:重要知识点

异常处理的代码简单,重要还是理解其思想

一.概述:

异常就是在运行时发生的不正常情况

  1. Throwable:
  2. Error
  3.     通常出现重大问题如,运行的类不存在或者内存溢出等
  4.     不编写针对代码对其处理
  5. Exception
  6.     在运行时运行出现的一起情况,可以通过trt catch finally
Throwable:

Error
    通常出现重大问题如,运行的类不存在或者内存溢出等
    不编写针对代码对其处理

Exception
    在运行时运行出现的一起情况,可以通过trt catch finally

Exception和Error的子类名都是以父类名作为后缀
异常举例:数组越界、传递参数错误啊。。。

  1. class Futime
  2. {
  3. }
  4. class Bigtime
  5. {
  6. }
  7. public class Main
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         /*int[] a = new int[5];
  12.          * a  = null;
  13.         System.out.println(a[5]);//异常
  14.         */
  15.         sleep(-5);
  16.     }
  17.     /*public static void sleep(int t)
  18.     {
  19.         if(t<0)
  20.         {
  21.             //数据出错,处理办法
  22.             //数据出错,处理办法
  23.             //数据出错,处理办法
  24.             //…..阅读性差
  25.             修正见下
  26.         }
  27.         else if(t>10000)
  28.         {
  29.             //数据出错,处理办法
  30.             //数据出错,处理办法
  31.             //数据出错,处理办法
  32.             new Bigtime();
  33.         }
  34.         else {
  35.             System.out.print(“休息”+t+”分种”);
  36.         }
  37.     }*/
  38.     public static void sleep(int t)//这样就清晰很多
  39.     {
  40.         if(t<0)//但是注意就有个问题,谁在调用这个功能,如何收到异常呢
  41.         {//采用抛出的方式
  42.              抛出  new Futime();//代表时间为负情况,这个对象中包含着问的名称,信息,位置等信息
  43.         }
  44.         else if(t>10000)//比如:我感冒了
  45.         {
  46.                  抛出  new Bigtime();//C是吃药,上医院…,而java就可以理解找治感冒的方法
  47.         }
  48.         else {
  49.             System.out.print(“休息”+t+”分种”);
  50.         }
  51.     }
  52. }
class Futime
{

}
class Bigtime
{

}
public class Main
{
	public static void main(String[] args)
	{
		/*int[] a = new int[5];
		 * a  = null;
		System.out.println(a[5]);//异常
		*/
		sleep(-5);
	}
	/*public static void sleep(int t)
	{
		if(t<0)
		{
			//数据出错,处理办法
			//数据出错,处理办法
			//数据出错,处理办法
			//.....阅读性差
			修正见下

		}
		else if(t>10000)
		{
			//数据出错,处理办法
			//数据出错,处理办法
			//数据出错,处理办法
			new Bigtime();
		}
		else {
			System.out.print("休息"+t+"分种");
		}
	}*/
	public static void sleep(int t)//这样就清晰很多
	{
		if(t<0)//但是注意就有个问题,谁在调用这个功能,如何收到异常呢
		{//采用抛出的方式
		     抛出  new Futime();//代表时间为负情况,这个对象中包含着问的名称,信息,位置等信息
		}
		else if(t>10000)//比如:我感冒了
		{
	             抛出  new Bigtime();//C是吃药,上医院...,而java就可以理解找治感冒的方法
		}
		else {
			System.out.print("休息"+t+"分种");
		}
	}
}

* * 这就体现出JAVA和c的不同,C是用if判断然后写诸多的解决方法,而java中是用 * 类的形式对不正常的情况进行了描述和封装对象 * 描述不正常的情况的类,就称为异常类 * 以前正常流程代码和问题处理代码相结合 * 现在将正常流程代码和问题处理代码相分离,提高阅读性 * 其实异常就是java通过面向对象的思想将问题封装成了对象 * 用异常类对其进行描述 * * 总结一句话就是不同的问题用不同的类进行具体的描述,比如角标越界,空指针 *

 


二.异常体系
如果问题很多就意味着描述的类以前很多 将其共性进行向上抽取,就形成了异常体系 最终问题(不正常情况)就分成了两大类
Throwable://无论是error,还是异常问题,问题发生就应该可以抛出,让调用者知道并处理 ->可抛性
(父类下面有两大派 ) 1.一般不可处理的。用Error类表示 2.可以处理的。用Exception
体系特点: Throwable及其所有子类都具有可抛性,不是这体系的不行
如何体现可抛性?
通过两个关键字:throws和throw,凡是被这两个关键字所操作的类和对象都具备可抛性 (Error和Exception类似于疾病,可以治愈的,和不可以治愈的) Error: 特点:是由JVM抛出的严重性的问题,已经影响到程序的运行了 这种问题的发生一般不针对性处理,因为处理不了,直接修改程序(int[] a = new int[1024*1024*800],开辟一个800M的数组)
Exception的直接子类很多,Error的直接子类不多,但是都是不可处理的,比较狠 体系的特点: 子类的后缀名都是用其父类名作为后缀,阅读性强,比如OutOfMemoryError

三、原理及其异常对象的抛出

小演示:

  1. class ExceptionDemo
  2. {
  3.     public int method(int[] arr,int index)
  4.     {
  5.         if(arr==null)
  6.             throw new NullPointerException(“数组引用怎么能为空呢?”);
  7.         if(index>=arr.length)
  8.         {
  9.             //return -1;为什么不写return,因为跟本就不运算
  10.         throw new ArrayIndexOutOfBoundsException(“数组角标越界”+index);
  11.         //throw new ArrayIndexOutOfBoundsException(“”+index);//默认的
  12.         }
  13.         if(index<0)
  14.         {
  15.         throw new ArrayIndexOutOfBoundsException(“数组角标不能为负”+index);
  16.         }
  17.         return arr[index];
  18.     }
  19. }
  20. public class Main
  21. {
  22.     public static void main(String[] args)
  23.     {
  24.         int[] a = new int[10];
  25.         ExceptionDemo C = new ExceptionDemo();
  26.         int num = C.method(a,20);
  27.         //int num = C.method(null,2);
  28.         System.out.print(“num”+num);
  29.     }
  30. }
class ExceptionDemo
{
	public int method(int[] arr,int index)
	{
		if(arr==null)
			throw new NullPointerException("数组引用怎么能为空呢?");
		if(index>=arr.length)
		{
			//return -1;为什么不写return,因为跟本就不运算
		throw new ArrayIndexOutOfBoundsException("数组角标越界"+index);
		//throw new ArrayIndexOutOfBoundsException(""+index);//默认的
		}
		if(index<0)
		{

		throw new ArrayIndexOutOfBoundsException("数组角标不能为负"+index);
		}
		return arr[index];

	}
}
public class Main
{
	public static void main(String[] args)
	{
		int[] a = new int[10];
		ExceptionDemo C = new ExceptionDemo();
		int num = C.method(a,20);
		//int num = C.method(null,2);
		System.out.print("num"+num);
	}	
}

四、自定义异常类及其抛出
类似于负数角标异常这种情况在java中并没有定义,那就可以按照java异常的创建思想,将负数角标异常这情况进行自定义描述,并封装称对象(自定义异常)
PS:如果让一个类称为异常类,那么这个类必须继承异常体系,只有异常体系的子类才具备可抛性,才可以被throw,throws两个关键字操作

 

  1. class FuException extends Exception
  2. {
  3.     FuException()
  4.     {
  5.     }
  6. }
  7. //定义一个异常后,需要告诉调用者可能出现的问题
  8. class ExceptionDemo
  9. {//要么捕捉,要么抛出
  10.     public int method(int[] arr,int index) throws FuException
  11.     {
  12.         if(index<0)
  13.         {
  14.         throw new FuException();
  15.         }
  16.         return arr[index];
  17.     }
  18. }
  19. public class Main
  20. {
  21.     public static void main(String[] args)throws FuException
  22.     {
  23.         int[] a = new int[10];
  24.         ExceptionDemo C = new ExceptionDemo();
  25.         int num = C.method(a,-2);
  26.         System.out.print(“num”+num);
  27.     }
  28. }
class FuException extends Exception
{
	FuException()
	{

	}
}
//定义一个异常后,需要告诉调用者可能出现的问题
class ExceptionDemo 
{//要么捕捉,要么抛出
	public int method(int[] arr,int index) throws FuException
	{

		if(index<0)
		{

		throw new FuException();
		}
		return arr[index];
	}
}
public class Main
{
	public static void main(String[] args)throws FuException
	{
		int[] a = new int[10];
		ExceptionDemo C = new ExceptionDemo();
		int num = C.method(a,-2);
		System.out.print("num"+num);
	}	
}

上述两个throws就对应了以上图示
五、编译时检测异常和运行时异常的区别
异常的分类:
1.编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException体系 ps:这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式,这样的问题都可以针对性的处理
2.编译时不检测异常(运行时异常):就是RuntimeException和其子类->编译器根本就不检测这个体系 ps:这种问题的发生无法让功能继续,运算无法进行,更多是因为调用者的原因导致的或者引发了内部状态的改变导致的 那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用的程序强制停止,让调用者对代码修改 (开发常碰见的异常)
所以自定义异常时,要么定义Exception,要么RuntimeException
throw和throws的区别
1.throws使用在函数上    throw使用在函数内 2.throws抛出的是异常类,可以抛出多个,用逗号隔开    throw抛出的是对象