Java-5-异常

Ø 课堂讨论要点:

 

1. 异常与错误

a. 错误(error)

语法错误, 逻辑错误 

      错误是系统的设计缺陷,必须返回软件公司修复。

 

b. 异常(exception)

   现代软件在运行过程中,往往容易受到周边因素的影响,比如网络意外中断,磁盘文件被误删除,用户录入了不正确的数据,导致软件运行中断,我们把这种不是因为软件自身原因,而是软件运行环境的意外,导致的运行停止,我们把其叫做异常。

 

   异常不是软件本身的问题,软件本身没有语法和逻辑问题,而是周边环境发生了意外,是软件设计者始料不及的。对于异常而言,软件开发者必须做好恰当的异常处理工作,比如当系统发现网络意外终止,应把程序挂起,提示用户异常原因,引导用户解决异常,解决完毕后,将积极重新尝试未完成操作,使程序能够得以继续运行。

 

 

2. 异常处理几点说明

     异常对象在系统运行过程中自动创建,当然所创建的对象类型是根据不同类型的异常而有差异的,系统会把出现异常的具体信息以及导致异常的原因写在这个对象中。

 

异常对象主要有两个属性:

     a. detailMessage   该异常的具体信息

     b. cause           说明了导致这个异常的原因。

                    

 异常位置的跟踪和定位:

     e. printStackTrace();  

     由于try块中可能有多条语句,异常爆发了,对象被抛出了,到底是那条语句导致的呢?

  printStackTrace()会详细的告诉你对应的位置。

 

  异常被捕获处理后,异常try…catch..结构后的语句将继续运行,程序不会中止。

  但是try块中发生异常的语句之后的语句将不再运行。

 

1. try

       try块中一般放置编程人员认为最可能抛出异常的高危语句,try块中的语句如果抛出了异常,那么该语句之后的语句将不再运行(try块中). 该语句抛出的异常,将经过catch模组过滤后,被处理或者无法处理被抛出方法。

       为了提高程序,请尽量减少try块体积,有些不必要的语句,不要放在try块中。

       

2. Catch块的级联

  由于一个try块中的语句,可能会抛出多种异常,对于程序员来说,应该尽早对其有预期,部署多个catch块对异常进行捕获处理,以免造成程序停机。

   但可抛出的异常种类实在难以预测,难免有漏网之鱼,所以,一般来说,我们catch级联块的最后会安排一个“万能”异常捕获者,来抓捕所有的漏网之鱼。

   Java.lang.Exception是万能捕获者,其是所有异常类的父类,根据父类引用变量可以指向所有子类对象的原则,其一般放在catch级联块最后一环对遗漏的异常进行捕获。 但其处理缺乏针对性,较为粗糙,建议具体的异常还是用具体的捕获者来捕获处理。

   Exception catch模组应避免放在级联块的第一个,否则后头的catch模组因代码无法到达,将全部失效,编译器将敏锐地发现这个问题,导致编译失败。

 

3. Finally

 

   坚强的finally块,其只怕System.exit(0), 无论try块中有异常,无异常,有无漏网之鱼均可运行,也不怕try中中途return.

   

   为了有效的释放在try语句运行过程中申请的资源,finally块为此而成。

 

   一般语句结构如下:

a. try…catch…finally

b. try…finally..

c. try..catch…

 

 

异常的基本处理原则:

   一个方法内的异常,要么抛出,要么捕获。如果本方法不适合处理,则可抛给上级方法处理,层层上报,直到合适的方法处理掉这个异常。

 

4. throwthrows

throw 抛出

通常情况下,是由系统帮助我们创建对应的异常对象,然后抛出。

在有些特定情况下,我们也可以手动创建异常对象,手动抛出!

 

if(b<0) throw new RuntimeException("b不能为负数!",null);

 

一个方法的最后部分将用来声明这个方法会抛出什么样的异常,以方便调用该方法的方法对其做好提前准备。一个方法可能抛出的异常不止一个,所以必须用throws

 

Xxxx methodName throws AException,BException…..

 

如果一个类中会抛出异常,该异常并不在这个类中被捕获,就必须做好方法的异常声明工作。

 

一个异常要么被捕获处理,要么被抛出,如被抛出方法,则方法必须声明异常信息。

 

任何一个方法,将默认在其签名上写上 throws RuntimeException;

 

 

In the following code, which lines will be printed on the standard output?

public class Test

{

public void method1(int x) throws Exception

{

try

{

method2(x); 

System.out.println("Checkpoint 1");

}

finally

{

System.out.println("Checkpoint 2");

}

System.out.println("Checkpoint 3");

}

public void method2(int x) throws Exception

{

if (x < 0)

{

throw new NegativeArraySizeException();

}

}

static public void main(String[] args) throws Exception

{

Test t = new Test();

t.method1(-55);

System.out.println("Checkpoint 4");

}

}

A. Checkpoint 1

B. Checkpoint 2

C. Checkpoint 3

D. Checkpoint 4

 

5. 运行时异常和检查异常

 

a. 运行时异常(Runtime Exception)

所有运行时异常 extends RuntimeException.

运行时异常是在程序运行过程中爆发的异常,其相对影响面小,问题不严重,一般不影响程序继续运行。

运行时异常无需捕获,自动抛出,也无需throws声明。

 

b. 检查异常  (checked Exception)

检查异常 extends Exception

其要么try…catch..捕获,要么在方法签名末尾显式申明抛出。

 

6. 自定义异常

 

 

Given:

11.classA {

12. public void process() { System.out.print(”A “); } }

13. class B extends A {

14. public void process() throws NullPointerException {

15. super.process();

16. if (true) throw new NullPointerException ();

17. System.out.print(“B”); }}

18. public static void main(String[] args) {

19. try { ((A)new B()).process(); }

20. catch (Exception e) { System.out.print(”Exception “); }

21. }}

What is the result?

Choose One

A. Exception

B. A Exception

C. A Exception B

D. A B Exception

E. Compilation fails because of an error in line 14.

F. Compilation fails because of an error in line 19.

 

答案:B

NullPointerException也是RuntimeException, 所以此处不认为子类重写方法抛出了比父类对应方法更多的异常。


 

Question 15

Click the Exhibit button.

Class TestException

1. public class TestException extends Exception {

2. }

Class A:

1. public class A {

2.

3. public String sayHello(String name) throws TestException {

4.

5. if(name == null) {

6.    throw new TestException();

7. }

8.

9. return “Hello “+ name;

10. }

11.

12. }

A programmer wants to use this code in an application:

45. A a=new A();

46. System.out.println(a.sayHello(”John”));

Which two are true? 

(Choose TWO)

A. Class A will not compile.

B. Line 46 can throw the unchecked exception TestException.

C. Line 45 can throw the unchecked exception TestException.

D. Line 46 will compile if the enclosing method throws a TestException.

E. Line 46 will compile if enclosed in a try block, where TestException

is caught.

 

答案: DE

检查异常TestException要么显式抛出或者捕获处理。

 

Analyze the following two classes

1. class First 

2. {

3.      static int a = 3;

4.  }

5.

6.  final class Second extends First 

7. {

8.      void method() 

9. {

10.       System.out.println(a);

11.     }

12. }

Choose One

A. Class First compiles, but class Second does not.

B. Neither class compiles.

C. Both classes compile, and if method() is invoked, it writes 3 to the output.

D. Both classes compile, and if method() is invoked, it throws an exception.

 

答案;C

静态属性也将被子类继承。

访问静态属性,最好直接直接使用类名,但使用this也是可以的,只是不推荐,容易在阅读代码过程中产生混淆。

 

Question 11

What will be the result of attempting to compile and run the following code?

 

Object o1 = new Object();

Object o2 = new Object();

o1 = o2;

if(o1.equals(o2)) { System.out.println("Equals"); }

 

Choose One

A. Compile time error

B. "Equals"

C. No output

D. Run time error

 

答案: B

因为o1o2指向了同一个对象,objectequals方法,只有在自己和自己比较时才相等。

 

 

6. 几点额外说明

a. 重写方法不能抛出比父类方法更多的异常,可以少抛出,但不可多抛出!

b. 重写方法不能在访问修饰符上比父类方法严格,可以放松要求,但不能提高访问限制。

父类是private , 子类可以是public,

父类是public , 子类不能是private.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值