`

黑马程序员_面向对象(三)_异常

 
阅读更多

 ------- android培训java培训、期待与您交流! ----------

 

 

 异常:

 

 

异常:就是程序在运行时出现不正常情况。

 

 

异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述。并封装成对象。其

 

 

实就是java对不正常情况进行描述后的对象体现。

 

 

对于问题的划分:

 

 

两种:一种是严重的问题,一种非严重的问题。

 

 

对于严重的,java通过Error类进行描述。对于Error一般不编写针对性的代码对其进行处理。

 

 

对与非严重的,java通过Exception类进行描述。对于Exception可以使用针对性的处理方式进行处理。

 

 

异常的处理:

 

 

try

{

    //需要被检测的代码;

}

catch(异常类变量)

{

    //处理异常的代码;(处理方式)

}

finally

{

    //一定会执行的语句;

}

 

 

对多异常的处理:

 

 

1、声明异常时,建议声明更为具体的异常。这样处理的可以更具体。

 

 

2、对方声明几个异常,就对应有几个catch块。不要定义多余的catch块。如果多个catch块中的异常出现继

 

 

承关系,父类异常catch块放在最下面。

 

 

class Demo{
	
	//在功能上通过throws的关键字声明了该功能有可能会出现问题
	int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException {

		int[] arr = new int[a];

		System.out.println(arr[4]);

		return a/b;
	}
}


class  ExceptionDemo2 {
	
	public static void main(String[] args) //throws Exception
	{
		Demo d = new Demo();
		try
		{
			int x = d.div(5,0);
			System.out.println("x="+x);
		}

		catch (ArithmeticException e)
		{
			System.out.println(e.toString());
			System.out.println("被零除了!!");

		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			System.out.println(e.toString());
			System.out.println("角标越界啦!!");
		}
		catch(Exception e)//父类要放在最后
		{
			System.out.println("hahah:"+e.toString());
		}

		System.out.println("over");

	}
}

 

 

当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。
 

要么在内部try catch处理。
 

要么在函数上声明让调用者处理。
 

 

一般情况在,函数内出现异常,函数上需要声明。

 

 

 

如何定义异常信息:

 


因为父类中已经把异常信息的操作都完成了。

 


所以子类只要在构造时,将异常信息传递给父类通过super语句。

 


那么就可以直接通过getMessage方法获取自定义的异常信息。

 

 

继承Exception原因:

 


异常体系有一个特点:因为异常类和异常对象都被抛出。

 


他们都具备可抛性。这个可抛性是Throwable这个体系中独有特点。

 

 

throws和throw的区别:

 


throws使用在函数上。

 


throw使用在函数内。

 

 

throws后面跟的异常类。可以跟多个。用逗号隔开。

 


throw后跟的是异常对象。
 

 

Exceptoin中有一个特殊的子类异常RuntimeException 运行时异常:

 

 

如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。

 

 

如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过;

 

 

之所以不用在函数声明,是因为不需要让调用者处理。

 


当该异常发生,希望程序停止。因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码

 

 

进行修正。

 

 

对于异常分两种:

 


1、编译时被检测的异常。


 
2、编译时不被检测的异常(运行时异常。RuntimeException以及其子类)

 

异常练习:

//蓝屏异常
class LanPingException extends Exception
{
	LanPingException(String message)
	{
		super(message);
	}
}
//冒烟异常
class MaoYanException extends Exception
{
	MaoYanException(String message)
	{
		super(message);
	}
}

//无法上课异常
class NoPlanException extends Exception
{
	NoPlanException(String msg)
	{
		super(msg);
	}
}

class Computer
{
	private int state = 3;//电脑运行状态
	public void run()throws LanPingException,MaoYanException
	{
		if(state==2)
			throw new LanPingException("蓝屏了");
		if(state==3)
			throw new MaoYanException("冒烟了");

		System.out.println("电脑运行");
	}
	public void reset()//重启电脑方法
	{
		state = 1;
		System.out.println("电脑重启");
		
	}
}

class Teacher
{
	private String name;
	private Computer cmpt;

	Teacher(String name)
	{
		this.name = name;
		cmpt = new Computer();

	}

	//老师讲课方法
	public void prelect()throws NoPlanException
	{
		try
		{
			cmpt.run();//老师启动电脑			
		}
		catch (LanPingException e)
		{
			cmpt.reset();//老师重启电脑
		}
		catch (MaoYanException e)
		{
			
			test();
			throw new NoPlanException("课时无法继续"+e.getMessage());
			
		}
		System.out.println("讲课");
	}
	public void test()
	{
		System.out.println("练习");
	}

}



class ExceptionTest 
{
	public static void main(String[] args) 
	{
		Teacher t = new Teacher("毕老师");
		try
		{
			t.prelect();
		}
		catch (NoPlanException e)
		{
			System.out.println(e.toString());
			System.out.println("换老师或者放假");
		}
		
	}
}

 

 

finally代码块:定义一定执行的代码。通常用于关闭资源。

 

//第一个格式:

try

{

   

}

catch ()

{

}

 

//第二个格式:

try

{

   

}

catch ()

{

}

finally

{

 

}

 

//第三个格式:

try

{

   

}

finally

{

}

 

 

 

异常在子父类覆盖中的体现:

 


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

 

 

子类。

 

 

2、如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。

 


3、如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。如果子

 

 

类方法发生了异常。就必须要进行try处理。绝对不能抛。

 

 

异常练习:有一个圆形和长方形。都可以获取面积,对于面积如果出现非法的数值,视为是获取面积出现问

 

 

题。问题通过异常来表示。

 

class NoValueException extends RuntimeException//自定义异常
{
	NoValueException(String message)
	{
		super(message);
	}
}

interface Shape//获取面积的接口
{
	void getArea();
}

class Rec implements Shape//长方形
{
	private int len,wid;

	Rec(int len ,int wid)//throws NoValueException运行期异常可以不声明
	{
		if(len<=0 || wid<=0)//判断数据是否符合要求
			throw new NoValueException("出现非法值");

		this.len = len;
		this.wid = wid;
	}

	public void getArea()
	{
		System.out.println(len*wid);
	}
}


class Circle implements Shape//圆形
{
	private int radius;
	public static final double PI = 3.14;//圆周率常量

	Circle(int radius)
	{
		if(radius<=0)//判断数据
			throw new NoValueException("非法");
		this.radius = radius;
	}

	public void getArea()
	{
		System.out.println(radius*radius*PI);
	}
}




class  ExceptionTest1
{
	public static void main(String[] args) 
	{	
		
		Rec r = new Rec(3,4);
		r.getArea();

		Circle c = new Circle(-8);//负数传入导致异常

		System.out.println("over");
	
	}
}


 

 

 

 ------- android培训java培训、期待与您交流! ----------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics