• 异常的处理try-catch


    Java异常处理

    Java采用的异常处理机制,是将异常处理的程序代码集中在一起, 与正常的程序代码分开,使得程序简洁、优雅,并易于维护。
    * 异常的处理: 抓抛模型
    *
    *
    * 过程一 : 抛, 程序在执行过程中,一旦出现异常,就会在异常处生成一个对应异常类的异常对象,
    * 并将此对象抛出,
    * 一旦抛出对象后,其后代码就不会执行了
    *
    * 过程二: 抓 可以理解为异常的处理方式
    * a: try-catch-finally
    * b: throws

    try-catch-finally 处理

     * try{
     *
     *     可能出现异常的代码
     *
     * }catch(异常类型1,变量名1){
     *     //处理异常的方法1
     * }catch(异常类型2, 变量2){
     *     //处理方式2
     * }
     * ......
     *
     * finally{
     *     //一定会执行的代码
     * }
     *
     */


    try{

    ...... //可能产生异常的代码 }

    catch( ExceptionName1 e ){

    ...... //当产生ExceptionName1型异常时的处置措施 }

    catch( ExceptionName2 e ){
    ...... //当产生ExceptionName2型异常时的处置措施 }
    [ finally{
    ...... //无论是否发生异常,都无条件执行的语句 }]

    // finally 是可选择的 不是必须de
    // try把可能出现异常的代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常处理的对象,根据此对象的类型,去catch中匹配
    // 一旦try中的异常对象匹配到某一个catch时就进入这个catch中进行异常处理,一旦处理完成就跳出当前的tray-catch结构(在没有写finally)继续执行后面的结构
    // catch没有字父类关系,声明再上和下无所谓
    // 常用的异常处理发方式: String : getMessage()   e.printStackTrace()
        public static void main(String[] args) {
            try {
                String abc = "abc";
                Integer num  = Integer.parseInt(abc);
            }catch (NumberFormatException e){
                e.printStackTrace();
                System.out.println("碰到了异常别着急");
            }
            System.out.println("hello");
        }

    finally: 上面的无论是否获取到,都会执行下面的finally信息

    finally声明的是一定会被执行的代码, 不论catch是否执行了都会去执行finally的信息

       try{
                String abc = "abc";
               int num =  Integer.parseInt(abc);
                System.out.println(num);
            }catch (NumberFormatException e){  //正常错误获取
                System.out.println("This is num error");
            }catch (FactoryConfigurationError e){
                System.out.println(e);
            }finally {
                System.out.println("上面的获取到了");
            }
        }
    输出:
    // This is num error
    //上面的获取到了

    catch未获取到错误信息 finally也输出

      public static void main(String[] args) {          
          try{                                          
              String abc = "abc";                       
             int num =  Integer.parseInt(abc);          
              System.out.println(num);                  
          }catch (IllegalAccessError e){                
              System.out.println("This is num error");  
          }catch (FactoryConfigurationError e){         
              System.out.println(e);                    
          }finally {                                    
              System.out.println("上面的错误未获取到");          
          }                                             
      } 
    //上面的错误未获取到

    异常处理方式二:

    throws+异常类型

       public static void main(String[] args) {                       
           try{                                                       
                method2();                                            
           }catch(NumberFormatException e){                           
               System.out.println("碰到了问题");                           
           }                                                          
       }                                                              
                                                                      
        public static void method2() throws NumberFormatException{    
                                                                      
             method1();                                               
            System.out.println("pengdaole ");                         
                                                                      
         }                                                            
         public static void method1(){                                
           String abc = "abc";                                        
           int num = Integer.parseInt(abc);                           
       }                                                              
                                                                      
                                                                      

    体会:

    try catch 是真正的处理异常,而throws只是把异常抛给了方法的调用者,并没有将异常处理掉

    .

  • 相关阅读:
    我的CISSP备考之路
    【CISSP备考笔记】第8章:软件开发安全
    【CISSP备考笔记】第7章:安全运营
    【CISSP备考笔记】第6章:安全评估与测试
    【CISSP备考笔记】第5章 身份与访问管理
    【CISSP备考笔记】第4章:通信与网络安全
    【CISSP备考笔记】第3章:安全工程
    【CISSP备考笔记】第2章:资产安全
    【CISSP备考笔记】第1章:安全与风险管理
    PHP后门隐藏技巧
  • 原文地址:https://www.cnblogs.com/zhaoyunlong/p/11768397.html
Copyright © 2020-2023  润新知