最愛Java

          書山有路勤為徑,學海無涯苦作舟

          《AspectJ Cookbook》讀書筆記五: 捕獲異常處理上的連接點

              在Java中拋出異常時,會將其向上傳遞給調用者,直到它被作為try/catch塊一部分的catch語句處理或者到達Java運行庫并在控制臺引發一條混亂的消息。如果捕獲到異常,就應該將異常作為一個對象傳遞給catch塊做合適的處理。同時,還有一種可能需要方面作為橫切行為做一部分事情,或者替代catch塊的正常行為。
          一. 捕獲何時捕捉異常
              使用handler(TypePattern)切入點。其語法如下:
              pointcut <pointcut name>(<any values to be picked up>) : handler(<class>):
           
             handler(TypePattern)切入點具有5個關鍵特征:
             1. handler(TypePattern)在捕獲異常的作用域內選擇連接點。
             2. handler(TypePattern)切入點的通知僅用于類型模式指定Throwable或其子類的地方。
             3. TypePattern聲明無論何時捕捉到異常或其子類的匹配模式,都會應用相應的通知。
             4. handler(TypePattern)切入點只支持before()形式的通知。這意味著不能使用像around()這樣的通知來重寫catch塊的正常行為。
             5. TypePattern可以包含通配符,用于選擇不同類上的一系列連接點。
          帶有通配符的TypePattern 描述
          mypackage..* 捕獲mypackage包及其子包中的連接點類
          MyClass+ 捕獲MyClass類及其任何子類中的連接點

              下面的例子展示了捕獲任何類拋出MyException類異常:

          package com.aspectj;

          public aspect HandlerRecipe {
              
          /**
               * Specifies calling advice when any exception object
               * is caught that matches the following rules for its 
               * type pattern;
               * 
               * Type:MyException
               
          */

              pointcut myExceptionHandlerPointcut() : handler(MyException);
              
              
          //Advice declaration
              before() : myExceptionHandlerPointcut() {
                  System.out.println(
          "------------------- Aspect Advice Logic -------------------");
                  System.out.println(
          "In the advice picked by " + "myExceptionHandlerPointcut()");
                  System.out.println(
          "Signature: " + thisJoinPoint.getStaticPart().getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
                  System.out.println(
          "------------------------------------------");
              }

          }


          二. 捕獲拋出的異常
              結合使用args([Types | Identifiers])切入點 與handler(TypePattern)切入點,將捕獲的異常展示為切入點上的標識符,可將其傳遞給相應的通知。
          package com.aspectj;

          public aspect AccessThrownException {
              pointcut myExceptionHandlerPointout(MyException exception) : handler(MyException) 
          && args(exception);
              
              
          //Advice declaration
              before(MyException exception) : myExceptionHandlerPointout(exception) {
                  System.out.println(
          "------------------- Aspect Advice Logic -------------------");
                  System.out.println(
          "In the advice picked by " + "myExceptionHandlerPointcut()");
                  System.out.println(
          "Signature: " + thisJoinPoint.getStaticPart().getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
                  System.out.println(
          "Exception caught:");
                  exception.printStackTrace();
                  System.out.println(
          "------------------------------------------");
              }

          }
             

          posted on 2008-07-11 09:28 Brian 閱讀(1617) 評論(1)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

          評論

          # re: 《AspectJ Cookbook》讀書筆記五: 捕獲異常處理上的連接點 2008-07-12 10:54 大梅沙云頂天海會所

          nnn
            回復  更多評論   

          公告


          導航

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汽车| 渭南市| 岑巩县| 清徐县| 登封市| 佛学| 信宜市| 明星| 辉南县| 简阳市| 阳泉市| 红桥区| 襄垣县| 阳原县| 历史| 石渠县| 仁寿县| 吉安县| 巩义市| 安吉县| 繁昌县| 都兰县| 正阳县| 龙川县| 临颍县| 郎溪县| 波密县| 平远县| 五寨县| 新化县| 五河县| 万山特区| 阆中市| 邢台市| 潞西市| 化隆| 廊坊市| 宝丰县| 安化县| 巴塘县| 安康市|