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)
    程序正常结束了
  • 相关阅读:
    软件测试
    python集合
    python基础(一)
    python布尔(空值)
    2013-12-15
    卸载IE9,IE10,IE11
    <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
    Log4j.properties配置详解
    WinCVS提交时出现错误 cvs server: C:/WINDOWS/TEMP/cvs**.tmp: No such file or directory 的解决方案
    Available Memory Is Low
  • 原文地址:https://www.cnblogs.com/penguin1024/p/11804964.html
Copyright © 2011-2022 走看看