JAVA自学教程之异常的应用和注意事项(三))



JAVA自学教程之异常的应用和注意事项(三))

十、异常的应用

  1. import java.util.Random;
  2. import javax.rmi.CORBA.Stub;
  3. /*
  4. *老师用电脑上课
  5. *用面向对象思考问题
  6. *问题设计两个对象
  7. *老师,电脑
  8. *
  9. *可能发生的问题:
  10. *电脑蓝屏、电脑冒烟
  11. */
  12. class Lan extends Exception//蓝屏
  13. {
  14.     Lan(String problem)
  15.     {
  16.         super(problem);
  17.     }
  18. }
  19. class Fir extends Exception//冒烟
  20. {
  21.     Fir(String problem)
  22.     {
  23.         super(problem);
  24.     }
  25. }
  26. class No extends Exception
  27. {
  28.     No(String problem)
  29.     {
  30.         super(problem);
  31.     }
  32. }
  33. class Computer
  34. {//定义一个电脑运行的状态
  35.     private int stu = 3; //stu为1正常,2是蓝屏,3是冒烟
  36.     //stu = Random()%2+1;
  37.     public void run()throws Lan,Fir
  38.     {
  39.         switch (stu)
  40.         {
  41.         case 2:
  42.             throw new Lan(“Computer is Lan!!!”);
  43.             //此处就不用break了,因为已经抛出了
  44.         case 3:
  45.             throw new Fir(“Computer is Fir!!!”);
  46.         case 1:
  47.             System.out.println(“Computer is running”);
  48.             break;
  49.         }
  50.     }
  51.     public void rest()//电脑重启
  52.     {
  53.         stu = 0;
  54.         System.out.println(“Computer is rest”);
  55.     }
  56. }
  57. class Teacher
  58. {
  59.     private String name = null;
  60.     private Computer C;
  61.     Teacher(String name)
  62.     {
  63.         this.name = name;
  64.         C = new Computer();
  65.     }
  66.     public void lesson()throws Fir,No
  67.     {
  68.         try
  69.         {
  70.             C.run();
  71.             System.out.println(name+” teacher is teaching”);
  72.         }
  73.         catch(Lan a)
  74.         {
  75.             System.out.println(a.toString());//打印信息
  76.             C.rest();
  77.             lesson();
  78.         }
  79.         catch(Fir b)
  80.         {
  81.             System.out.println(b.toString());//打印信息
  82.             test();
  83.             //能处理的已经处理了,处理不了的继续向外抛
  84.             //找人,对电脑进行维修,并告知学校并带原因,不告知,就是异常异常
  85.             throw new No(“课时无法进行,”+b.getMessage());
  86.         }
  87.     }
  88.     public void test()//做练习
  89.     {
  90.         System.out.println(“student is exciesing”);
  91.     }
  92. }
  93. public class Main
  94. {
  95.     public static void main(String[] args)
  96.     {
  97.         Teacher B = new Teacher(“BLF2″);
  98.         try
  99.         {
  100.             B.lesson();
  101.         }
  102.         catch (Fir e)
  103.         {
  104.             System.out.println(“…..”);
  105.         }
  106.         catch (No e)
  107.         {
  108.             System.out.println(e.toString());
  109.         }
  110.         finally
  111.         {//无论课上发生什么,但是还是会下课
  112.             System.out.println(“到点下课”);
  113.         }
  114.     }
  115. }
import java.util.Random;

import javax.rmi.CORBA.Stub;

/*
 *老师用电脑上课
 *用面向对象思考问题
 *问题设计两个对象
 *老师,电脑
 *
 *可能发生的问题:
 *电脑蓝屏、电脑冒烟
 */
class Lan extends Exception//蓝屏
{
	Lan(String problem)
	{
		super(problem);
	}
}
class Fir extends Exception//冒烟
{
	Fir(String problem)
	{
		super(problem);
	}
}
class No extends Exception
{
	No(String problem)
	{
		super(problem);
	}
}
class Computer
{//定义一个电脑运行的状态
	private int stu = 3; //stu为1正常,2是蓝屏,3是冒烟
	//stu = Random()%2+1;
	public void run()throws Lan,Fir 
	{
		switch (stu) 
		{
		case 2:
			throw new Lan("Computer is Lan!!!");
			//此处就不用break了,因为已经抛出了
		case 3:
			throw new Fir("Computer is Fir!!!");
		case 1:
			System.out.println("Computer is running");
			break;
		}

	}
	public void rest()//电脑重启
	{
		stu = 0;
		System.out.println("Computer is rest");
	}
}
class Teacher
{
	private String name = null;
	private Computer C;
	Teacher(String name) 
	{
		this.name = name;
		C = new Computer();
	}
	public void lesson()throws Fir,No
	{
		try 
		{
			C.run();
			System.out.println(name+" teacher is teaching");
		} 
		catch(Lan a)
		{
			System.out.println(a.toString());//打印信息
			C.rest();
			lesson();
		}
		catch(Fir b)
		{
			System.out.println(b.toString());//打印信息
			test();
			//能处理的已经处理了,处理不了的继续向外抛
			//找人,对电脑进行维修,并告知学校并带原因,不告知,就是异常异常
			throw new No("课时无法进行,"+b.getMessage());

		}

	}
	public void test()//做练习
	{
		System.out.println("student is exciesing");
	}
}
public class Main
{
	public static void main(String[] args)
	{
		Teacher B = new Teacher("BLF2");
		try 
		{
			B.lesson();
		} 
		catch (Fir e)
		{
			System.out.println(".....");
		}
		catch (No e)
		{
			System.out.println(e.toString());

		}
		finally 
		{//无论课上发生什么,但是还是会下课
			System.out.println("到点下课");
		}

	}	
}

/* * 异常的转换,也就叫异常的封装 * 数据库 * class Nosuess extends Exception * { * …..//将异常转换,只需要告知出现异常 * } * void adddata()throws Nosuess * { * 1.连接数据库 * try * { * 添加数据,出现异常  SQLException,但是添加数据的人不知道SQLException是什么 * } * catch * { * 内部处理 * throw new Nosuess(); * } * finally * { * 关闭数据库 * } * } * */

 

 

十一、异常的注意事项

(1).子类在覆盖父类方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出子类的异常或者该异常的子类(见下述代码)

(2).如果父类抛出多个异常,那么子类只能抛出父类异常的子集


简单说:子类覆盖父类只能抛出父类的异常或者父类异常的子类或者子集

注意:如果父类的方法没有抛出异常,那么子类的覆盖时绝对不能抛

 

下述代码解释注意事项(1)

  1. class A extends Exception
  2. {}
  3. class B extends A
  4. {}
  5. class C extends Exception
  6. {}
  7. class FU
  8. {
  9.     void show()throws A
  10.     {
  11.     }
  12. }
  13. class Test
  14. {
  15.     void method(FU f)
  16.     {
  17.         try
  18.         {
  19.             f.show();
  20.         }
  21.         catch (A a)
  22.         {
  23.         }
  24.     }
  25. }
  26. class ZI extends FU
  27. {
  28.     void show()throws A,B //也可以不抛,但是不可以抛出其他异常
  29.     {}
  30. }
  31. public class Main
  32. {
  33.     public static void main(String[] args)
  34.     {
  35.         Test t = new Test();
  36.         t.method(new ZI());//用多态就可以解释为什么不能抛其他的异常
  37.     }
  38. }
 class A extends Exception
{}
class B extends A
{}
class C extends Exception
{}
class FU
{
	void show()throws A
	{
	}
}
class Test
{
	void method(FU f)
	{
		try
		{
			f.show();
		}
		catch (A a)
		{

		}
	}
}
class ZI extends FU
{
	void show()throws A,B //也可以不抛,但是不可以抛出其他异常
	{}
}
public class Main
{
	public static void main(String[] args)
	{
		Test t = new Test();
		t.method(new ZI());//用多态就可以解释为什么不能抛其他的异常
	}	
}

下述代码解释注意事项(2)

 

  1. interface in
  2. {
  3.     abstract void show();
  4. }
  5. class BB implements in
  6. {
  7.     public void show()//throws Exception,因为父类没有出现异常,子类就不能抛
  8.     {
  9.             //如果真的出现异常,那么只能try,不能抛
  10.     }
  11. }
interface in
{
	abstract void show();
}
class BB implements in
{
	public void show()//throws Exception,因为父类没有出现异常,子类就不能抛
	{
			//如果真的出现异常,那么只能try,不能抛
	}
}

异常到此就结束了,内容很多,自己需要多复习,消化才是。。。