Java异常使用

Java异常就是Java对程序运行时错误的处理。Java的所有异常都继承自java.lang.Throwable类。Java的异常处理框架如下:



在《EffectIve Java》(高效的Java)一书中关于异常有几点建议:

1、只针对异常的情况才使用异常

2、对可恢复的情况的使用受检异常,对编程错误使用运行时异常

3、避免不必要的使用受检的异常

4、优先使用标准的异常

5、抛出与抽象相对应的异常

6、每个方法抛出的异常都要有文档


//1.非受检异常,无抛出和捕获
    public static void main(String[] args) {
        testUncheckedException();
        System.out.println("next ...");
    }
    public static void testUncheckedException(){
        Integer.parseInt("I am not a number");
        System.out.println("then ...");
    }


输出结果:

start ...
Exception in thread "main" java.lang.NumberFormatException: For input string: "I am not a number"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:492)
	at java.lang.Integer.parseInt(Integer.java:527)
	at org.littleUtil.ExceptionDemo.testUncheckedException(ExceptionDemo.java:195)
	at org.littleUtil.ExceptionDemo.main(ExceptionDemo.java:191)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)



此段程序期望输出next ...和end ... 由于异常错误的发生,程序没有正常的进行和终止

//2.非受检异常,捕获异常,也可直接抛出,但会导致方法无法正常运行
    public static void main(String[] args){
        System.out.println("start ...");
        testUncheckedException2();

        try {
            testUncheckedException3();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println("end ...");
    }
    public static void testUncheckedException2(){
        try {
            Integer.parseInt("I am not a number");
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println("next ... in 2");
    }
    public static void testUncheckedException3() throws NumberFormatException{
        Integer.parseInt("I am not a number");
        System.out.println("next ... in 3");
    }

输出结果:

start ...
For input string: "I am not a number"
next ... in 2
For input string: "I am not a number"
end ...


//3.受检的异常
    public static void main(String[] args){
        System.out.println("start ...");
        try {
            testCheckedException();
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
        System.out.println("end ...");

    }
    public static void testCheckedException() throws IOException{
        File file = new File("D://notfound.txt");
        InputStream inputStream = new FileInputStream(file);
        inputStream.close();
    }

输出结果:

start ...
D:\notfound.txt (系统找不到指定的文件。)
end ...

//4.finally里不应该出现return
    public static void main(String[] args) {
        System.out.println("start ...");
        System.out.println(testFinally1());
        System.out.println("end ...");
    }
    public static String testFinally1(){
        try {
            if (true){
                throw new NumberFormatException("数字转换异常");
            }
        }catch (NumberFormatException e){
            System.out.println(e.getMessage());
            throw e;
        }finally {
            return "OK";
        }

    }


输出结果:

start ...
数字转换异常
OK
end ...

  //5.finally里不应该出现return
    public static void main(String[] args) {
        System.out.println("start ...");
        try {
            System.out.println(testFinally2());
        }catch (NumberFormatException e){
            System.out.println("main:"+e.getMessage());
        }
        System.out.println("end ...");

    }
    public static String testFinally2(){
        try {
            if (true){
                throw new NumberFormatException("数字转换异常");
            }
        }catch (NumberFormatException e){
            System.out.println(e.getMessage());
            throw e;
        }finally {
            System.out.println("finally ...");
        }
        return "OK";
    }

输出结果:

start ...
数字转换异常
finally ...
main:数字转换异常
end ...


4和5可以得出结论:如果finally里出现了return则,catch里抛出的异常不会被上一层捕获到。

在java异常机制中finally语句块里的代码始终都会被执行,如果其中出现return则表明程序正常返回,上一层则不会察觉到该段程序的异常问题。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值