最愛Java

          書山有路勤為徑,學(xué)海無涯苦作舟

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

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

              下面的例子展示了捕獲任何類拋出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(
          "------------------------------------------");
              }

          }


          二. 捕獲拋出的異常
              結(jié)合使用args([Types | Identifiers])切入點(diǎn) 與handler(TypePattern)切入點(diǎn),將捕獲的異常展示為切入點(diǎn)上的標(biāo)識(shí)符,可將其傳遞給相應(yīng)的通知。
          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 閱讀(1612) 評(píng)論(1)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

          評(píng)論

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

          nnn
            回復(fù)  更多評(píng)論   

          公告


          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 沛县| 中方县| 泸定县| 晋宁县| 息烽县| 安宁市| 扎兰屯市| 罗源县| 呼玛县| 枣庄市| 雷州市| 平湖市| 宝清县| 敦煌市| 安多县| 中牟县| 古交市| 荣成市| 漠河县| 武胜县| 英超| 丰县| 清丰县| 边坝县| 河北区| 张家口市| 郯城县| 长沙市| 荣昌县| 白河县| 景谷| 留坝县| 丹寨县| 祁连县| 荔波县| 东安县| 武夷山市| 大连市| 五家渠市| 芜湖县| 科技|