Home Contact

Igor Milovanović

.NET, cats and more...

News

Article Categories

Archives

Post Categories

Image Galleries

Blogs I read

Communities

Links

Syndication:

(Better) Exception Handling with AOP

One of the questions which raised in the TechEd BOF session was about the possibilities to improve exception management with AOP. Let's take Microsoft Exception Management Application Block for .NET as example. This building block provides ExceptionManager and a set of different publishers (you can write the exception to file, windows event log, send it per email etc...).

If you use this exception block you have to  catch the exceptions you want to publish and use the exception manager (a good place for doing this is a component or service boundary):

   1:  try
   2:  {
   3:    // Core concern of the method.
   4:  }
   5:  catch ( Exception ex )
   6:  {
   7:      ExceptionManager.Publish( ex );
   8:  }
   9:   

The problem is that you have to write this chunk of code into every method which uses the exception manager (crosscutting concern). This is not very elegant, and can be easily forgotten. With AOP, you would define a join point on exception and separate the exception management from core concern (AspectJ-like pseudo code:)

   1:  // Pointcut - AspectJ-Like
   2:  pointcut exceptionHandler(Exception e): handler(Exception+) && args(e);
   3:   
   4:  // advice for exception management
   5:  after(Exception e): exceptionHandler(e) 
   6:  {
   7:       ExceptionManager.Publish (e)
   8:  }
   9:   

[1] Microsoft Exception Management Application Block
[2] Swarr, R. Make your Apps Operations Friendly with AOP
[3] Ugurlu Ergün, Nichts geht mehr und Spass dabei, Fehlerbehandlung mit AOP

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, September 23, 2004 11:56 AM

Feedback

# re: (Better) Exception Handling with AOP

one of the problems, as I see it, of using a "Generic Exception Manager" is that the context where the exception was thrown is lost, what makes the work of handling the exception harder.

- a possible solution can be to design the Exception classes appropriately, which means that they will hold context information.

- another solution, where AOP comes into play, is to use the Wormhole pattern to make the context where the Exception was thrown available to the ExceptionManager.
10/18/2004 9:05 PM | Alexei

# re: (Better) Exception Handling with AOP

one of the problems, as I see it, of using a "Generic Exception Manager" is that the context where the exception was thrown is lost, what makes the work of handling the exception harder.

- a possible solution can be to design the Exception classes appropriately, which means that they will hold context information.

- another solution, where AOP comes into play, is to use the Wormhole pattern to make the context where the Exception was thrown available to the ExceptionManager.
10/18/2004 9:06 PM | Alexei

# re: (Better) Exception Handling with AOP

about extracting the context, when inside an advice, you have access to a special this variable named "thisJoinPoint" that gives you access to the context.

for example:

//example valid within an advice body
StringBuilder sb = new StringBuilder();
sb.append( "\n-------------- AOP Context ---------------\n" );
sb.append( "Pointcut : " + thisJoinPoint.toShortString() ).append( "\n" );
sb.append( "Source : " + thisJoinPoint.getStaticPart().getSourceLocation() ).append( "\n" );
String context = sb.toString();
System.out.println( context );

this is just a very simple example but "thisJoinPoint" gives you access to lots of context information, have a look at the the JoinPoint class in the JavaDoc 4/24/2009 5:59 PM | Pascal

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: