JAVA中关于异常需要注意的地方

JAVA异常处理的抛出异常的throwthrows这两个关键字的区别:throws写在定义一个方法名的后面,表示若该方法出现异常时将异常抛给调用他的地方;throw写在具体的方法内部,用来抛出异常进而可以被try...catch()语句捕获;

情况一:

try {

        new Exception();        

System.out.println("ok");

    } catch(Exception e) {

       System.out.println("exception");

    } finally {

       System.out.println("finally");

    }

打印结果:ok

Finally

说明:千万不要以为new Exception()会抛出异常,这里只是简单new了一个Exception实例而已。

情况二:

    try {

       throw new Exception();

       System.out.println("ok");

    } catch(Exception e) {

       System.out.println("exception");

    } finally {

       System.out.println("finally");

    }

结果:编译不通过,throw new Exception();语句后面System.out.println("ok");的区域都视为不可到达代码段Unreachable Code

情况三

    public class TestExceptionPrint {

    public static void aMethod() throws Exception {

       try {

           throw new Exception();

       } catch(Exception e) {

           System.out.println("Method Exception");

       } finally {

           System.out.println("finally");

       }

    }

    public static void main(String[] args) {

       try {

           aMethod();

           System.out.println("Main NoException!");

       } catch(Exception e) {

           System.out.println("exception!");

       }

       System.out.println("finished");

    }

}

打印结果:

Method Exception

finally

Main NoException!

finished

结果说明:aMehtod里面抛出了异常,但是由于方法里对其有进行捕获catch,所以异常不会传到main方法里,于是main方法就正常执行了。下面来看aMethod方法里没有对异常进行catch的情况:

情况四:

package com.yilong.scjp.test;

public class TestExceptionPrint {

        public static void aMethod() throws Exception {

           try {

               throw new Exception();

           } finally {

               System.out.println("finally");

           }

        }

        public static void main(String[] args) {

           try {

               aMethod();

               System.out.println("Main NoException!");

           } catch(Exception e) {

               System.out.println("exception!");

           }

           System.out.println("finished");

        }

}

打印结果:

finally

exception!

finished

结果说明:由于aMethod方法里没有对异常进行捕获,于是异常传递到了Main方法里。除此之外,还可以看到在try…catch里面一旦捕获到异常,那么在该块里面剩下的语句就不会再被执行到(Main NoException没有被打印),但是在出了try…catch语句块后就又会被执行了(finished被打印了)

还需要注意的是,异常类的结构:异常类的根类是Throwable,他的直接子类有Error,ExceptionError是系统的内部错误,一般程序员处理不了,如果程序中抛出Error的异常,编译是不能通过的。Exception则是我们可以处理的异常。Exception类里又分两类:一种是RuntimeException,一种是其他的ExceptionRuntimeException是一种经常会出现的错误,这种错误可以catch处理,也可以不catch,而剩下的Exception则是必须进行catchException。故对于上述的情况,如果在程序中抛出了异常后,既不进行捕捉,也不进行throws,那么就会报错,而下面的程序由于抛出的是不一定要捕捉的异常,故不会报错:

package com.yilong.scjp.test;

public class TestExceptionPrint {

        public static void aMethod() {

               throw new RuntimeException();

        }

        public static void main(String[] args) {

           try {

               aMethod();

               System.out.println("Main NoException!");

           } catch(Exception e) {

               System.out.println("exception!");

           }

           System.out.println("finished");

        }

}

另外,捕捉异常的时候需要注意异常的“大小”,比如:Exception包括了IOException,那么在捕获异常的时候就必须先捕捉IOException,如果先捕捉Exception,那么编译期间就会报错,因为Exception异常已经包括了IOException。下面给出实例:

class Exc0 extends Exception {}

class Exc1 extends Exc0 {}

public class TestOutterClass {

        public static void main(String args[]) {

           try {

               throw new Exc1();

           } catch (Exc0 e0) {

               System.out.println("Ex0 caught");

           } catch (Exception e) {

               System.out.println("exception caught");

           }

        }

}

打印结果:Ex0 caught

分析:如果先捕获Exception,那么编译就会报错。因为Exc0是从Exception继承的,所以Exception包含了Exc0;如果途中出现非Exc0的异常,那么打印结果就是:exception caught;

 补充:常见的RuntimeException有:NullPointerException, NumberFormatException(继承自IllegalArgumentException), ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException, ClassCastException,  UnSupportedOperationException, ArithmeticException, IllegalArgumentException

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值