20200331java学习之路之面向对象之异常

一:什么是异常?

  1. 在java运行时发生的不正常情况----就是异常
  2. 描述不正常情况的类----就是异常类
  3. 异常就是java在通过面向对象的思想将问题封装成对象,用异常类对其进行扫描
  4. 不同的问题用不同的类进行具体描述,如:空指针,角标越界
class ExceptionDemo{
	public static void main(String[] args){
		int [] arr=new int [3];
		Demo d=new Demo();
		d.method(arr,3);
		//arr=null;
		System.out.println(arr[2]);
	}
	
}
class Demo{
	public static void method(int [] arr,int index){
			System.out.println(arr[index]);
			if(arr==null)
				throw new NullPointerException("数组引用不能为空");
			if(index >=arr.length){
				throw new ArrayIndexOutOfBoundsException("shuzuyuejiela数组越界了 "+index);
			}
			if (index<0){
				throw new ArrayIndexOutOfBoundsException("shzujiaobiaobunengweifyushu数组角标不能为负数"+index);
			}
			return arr[index];
	}
}

二:异常问题的分类

  1. 异常多,意味着描述类也很多,将其共性进行向上抽取,形成了异常体系最终问题被分成两大类
    - 一类为Error
    - 另一类为Exception
  2. Error:JVM告诉调用者,一般不作针对性处理,直接修改程序代码
  3. Exception:发生并告调用者,做出针对性的处理
  4. 编译异常的分类:
    -----编译时被检测异常:只要是exception和其子类都是,除了特殊子类runtimeexception体系
    这种问题一旦出现,希望在编译时就进行检测,让这种问题对应的处理方式
    这样的问题都可以针对性的处理

    ----编译时不检测异常(运行时异常):就是exception中的runtimeexception和其子类
    这种问题发生,无法让功能据需,运算无法进行,更多是因为调用的原因导致或者引发内部状态的改变导致
    那么这种问题一般不处理,直接编译通过,但是运行时,让调用者用时的程序强制停止,让调用者对代码进行修正

(所以自定义异常,要么继承exception或者runtimeexception)

三:异常的处理方式

  1. 遇到问题不进行具体处理,抛给调用者,在语句中用throw,throws关键字进行声明异常

  2. throws 与 throw的区别
    -throws使用在函数上
    -throw使用在函数内

                   -throws抛出的是异常类,可以抛出多个,用逗号隔开
             -throw抛出的是异常对象。
    
  3. 异常处理捕捉的形式(可以进行针对性的处理)

  4. 具体格式:

try{
	
	需要被检测异常的代码
}
catch(异常类  变量){  //该变量用于接受发生异常的对象
	
	处理异常代码
	
}
finally{
	
	一定会被执行的代码
}
  1. try catch finally组合特点
1trycatchfinally
2try catch(多个)当没有必要资源需要释放时,可以不用定义finally
3try finally

void show()//throws Exception
{
	try{
		throw new Exception();
	}
	finally{
		//关闭资源
	}
}


/*catch(Exception e)
{
	
}
*/
class ExceptionDemo3{
	public static void main(String[] args){
		Demo d =new Demo();
		try{
			int [] arr=int [3];
			int num =d.method(arr,30);
			System.out.println("num="+num);

		}
		catch(FuShuIndexException e){
			System.out.println("fushujiaobiaoyichang");
			System.out.println("message:"+e.getMessage());
			e.printStackTrace();
			System.out.println("fushuyichang");
		}
		System.out.println("over");
	}
}

四:异常处理的原则

1,函数内容如果抛出需要检测的异常,那么函数上必须声明
否则必须在函数内用try catch捕捉,否则编译失败

2,如果调用到了声明异常的函数,要么用try catch要么throws,否则编译失败

3,什么时候catch,什么时候throws?
功能内容可以解决,用catch
解决不了用throws告诉调用者,由调用者解决

4,一个功能如果抛出了多个异常,那么调用时,必须有对应对个catch进行针对性的处理
内部有几个需要检测的异常,抛出几个就catch几个

clas Demo{
	public int  show(int index){
		if(index<0)
			throw new ArrayIndexOutOfBoundsException("yuejiela");

		int [] arr=new int[3];
		return arr[index];
	}
}



class ExceptionDemo4{
	public static void main(String[] args){
			Demo d=new Demo();
			try{
				int num=d.show(-3);
				System.out.println("num="+num);
			}
			catch(ArrayIndexOutOfBoundsException){
				System.out.println(e.toStrint());
				return;
			}
			finally{
				System.out.println("finally");
			}
			System.out.println("overs");
	}
}

五:异常的注意事项

1,子类覆盖父类方法时,父类的方法如果抛出了异常,
那么子类的方法只能抛出父类的异常或者该异常的子类

2,如果父类抛出多个异常,那么子类之类只能抛出父类异常的子集
简单说:子类覆盖父类,只能抛出父类的异常或者子类,或者子集
注意:如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,只能try

class A extends Exception{

}
class B extends A{

}
class C extends Exception{

}

Exception
|–A
|—B
|–C

class FuShuIndexExceptiono extends Exception{
	FuShuIndexException(){}
	FuShuIndexException(String msg){
		super(msg);
	}
}

class Demo{ 
	public int method (int [] arr,int index) throws FuShuIndexException
	{
		if(arr==null)
			throw new NullPointerException("shuzuyiyongbunengweikong");
		if(index>=arr.length){
			throw new ArrayIndexOutOfBoundsException("shuzujiaobiaoyuejie la "+index);
		}
		if(index<0){
			throw new FuShuIndexException("JIOABIAOBIANCHEGNFUSHULE");
		}
		return arr[index];
	}
}

class ExceptionDemo2{
	public static void main(String[] args) throws FuShuIndexException
 	{
 			int [] arr =new int [3];
 			Demo d=new Demo();
 			int num=d.method(arr,-30);
 			System.out.println("num="+num);
 			System.out.println("over");
	}
}




/*
class Person{
	private String name;
	Person(String name){
	this.name=name;
	}
	public getName(){
	return name;
	}
}
class Student extends Person{
	Student(String name){
	super(name);
	}
}

 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值