zoukankan      html  css  js  c++  java
  • 检测性异常(案例)

    看几段代码:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    // 检测异常 (编译期间产生异常)
    public class CheckExceptionTest {
    
        public static void main(String[] args) {
            
            FileInputStream fis =null;
            try {
                System.out.println("1");      
                
                fis = new FileInputStream("d:/nba.txt");    //找不到指定路径下的文件 ,则产生FileNotFoundException异常
                
                System.out.println("2");     
                
            }catch (FileNotFoundException e) {              
                
                System.out.println("3");            
                
                System.out.print("产生文件找不到异常:");
                e.printStackTrace();                      
                System.out.println("4");                
            }
            
            try {
                System.out.println("5");
                
                fis.close();                                       
                
                System.out.println("6");
                
            }catch(IOException e){
                   System.out.println("7");
                   System.out.print("产生IO流异常:");
                   e.printStackTrace();
                   System.out.println("8");
                   
                  }catch (NullPointerException e) {      
                   System.out.print("产生空指针异常:");
                   e.printStackTrace();
                  
                  
                 }
            
             
            System.out.println("程序正常结束了");     
        }
        
    
    }

    上面代码 要 分 3种情况讨论:

    1.若程序不产生异常,正常执行的情况:

    1
    2
    5
    6
    程序正常结束了

    2.若程序产生异常

    .        若在d:/nba.txt该路径下 不存在nba.txt文件,  则 产生 文件找不到异常

              且因为FileInputStream fis =null;  则 fis.close()方法 还会产生 空指针异常

    2.1没有处理产生的空指针异常  (即,把上面代码中的下面部分代码注释掉)

    //              catch (NullPointerException e) {      
    //               System.out.print("产生空指针异常:");
    //               e.printStackTrace();
    //              
    //              
    //

     结果:

    1
    3
    产生文件找不到异常:java.io.FileNotFoundException: d:
    ba.txt (系统找不到指定的文件。)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:131)
        at java.io.FileInputStream.<init>(FileInputStream.java:87)
        at com.monkey1029.CheckExceptionTest.main(CheckExceptionTest.java:16)
    4
    5
    Exception in thread "main" java.lang.NullPointerException
        at com.monkey1029.CheckExceptionTest.main(CheckExceptionTest.java:32)

    2.2 对产生的空指针异常进行了处理

    1
    3
    产生文件找不到异常:java.io.FileNotFoundException: d:
    ba.txt (系统找不到指定的文件。)
    4
    5
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:131)
        at java.io.FileInputStream.<init>(FileInputStream.java:87)
        at com.monkey1029.CheckExceptionTest.main(CheckExceptionTest.java:16)
    产生空指针异常:java.lang.NullPointerException
        at com.monkey1029.CheckExceptionTest.main(CheckExceptionTest.java:32)
    程序正常结束了
  • 相关阅读:
    Sublime 下配置vim模式 + VintageEx-master下载地址
    Potplayer快捷键
    Failed to fetch URl https://dl-ssl.google.com/android/repository/addo Android SDK更新以及ADT更新出现问题的解决办法
    最快速的Android开发环境搭建ADT-Bundle及Hello World
    Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
    X-Mirage苹果屏幕录制工具7天试用期破解 imsoft.cnblogs
    玩转博客园的心路总结
    玩转博客园的5个小技巧
    五个对你有用的Everything搜索技巧
    PS如何批量处理图片
  • 原文地址:https://www.cnblogs.com/penguin1024/p/11804964.html
Copyright © 2011-2022 走看看