Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Logging, but not catching exceptions

Title: Logging, but not catching exceptions

Hi Ian,

 

Even if you do name the throwable in the after throwing advice, it still won’t catch it. Here’s an example that logs an exception once when it is first encountered and lets it be handled up the calling tree:

 

import java.util.WeakHashMap;

 

public class AfterThrowingExample {

    public static void main(String args[]) {

        try {

             throwIt();

        } catch (Exception e) {

             System.err.println("Caught in main ");

        }

    }

    private static void throwIt() throws Exception {

        helper();

    }

    private static void helper() throws Exception {

        throw new Exception("boom");

    }

}

 

aspect TraceThrowing {

    WeakHashMap alreadySeen = new WeakHashMap();

 

    pointcut scope() : within(AfterThrowingExample);

 

    after() throwing (Throwable t) : scope() && execution(* *(..)) {

        // could also look for getCause()...

        if (alreadySeen.get(t) == null) {

            alreadySeen.put(t, ""); // register existence

            System.err.println("TODO: log occurance of "+t);

            //i.e., logger.log("error", t);

        }

    }

}

 

Example output:

TODO: log occurance of java.lang.Exception: boom

Caught in main

 


From: aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Orford, Ian
Sent: Wednesday, January 11, 2006 5:27 AM
To: 'aspectj-users@xxxxxxxxxxx'
Subject: [aspectj-users] Logging, but not catching exceptions

 

I'm using an "after() throwing :" advice that doesn't name/catch an exception.
I'm doing this because I want to intercept (but not catch) both declared and run-time exceptions and errors.

Is there anyway to get a hold of the thrown Throwable so that I can log it?

Alternatively, is there a way to use an "after() throwing (Throwable t) :" advice that doesn't actually catch the exception?

Thanks, Ian
 ------



--------------------------------------------------------------------------------
The information contained herein is confidential and is intended solely for the
addressee. Access by any other party is unauthorised without the express
written permission of the sender. If you are not the intended recipient, please
contact the sender either via the company switchboard on +44 (0)20 7623 8000, or
via e-mail return. If you have received this e-mail in error or wish to read our
e-mail disclaimer statement and monitoring policy, please refer to
http://www.drkw.com/disc/email/ or contact the sender. 3167
--------------------------------------------------------------------------------


Back to the top