Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] how exception handling works?

Hi Steve,

In order for you not being forced to handle the exception in the method methodSocket(), you need to use the softening exception capability of AspectJ. That will turn your checked exceptions into unchecked ones, which means you are not forced to handle the exception in your method. Then, you can do what you wished in your e-mail, in the aspect:

public aspect MyAspect {

declare soft : java.io.IOException : execution(void MyClass.methodSocket());

 void around() : call(void MyClass.methodSocket()) {
   try {
     proceed();
   } catch(Exception e) {
     System.out.println(e.getCause());
   }
 }
}

public class MyClass {
  void methodSocket() {
   socket.connect(new InetSocketAddress("www.google.de", 82), 200);
 }
}

If you want to know more about this or you want to "preserve" the checked exception, look in the AspectJ book, by Ramnivas Laddad, and read about the Exception introduction pattern.

Regards,

Paulo Zenida

Citando Steve Kirkaw <skirkaw@xxxxxxxxxxxxxx>:

Hi,

I am stuck with exception handling in aspectj.

Let's say in a method in the class I execute the following snippet:

void methodSocket() {

try {

socket.connect(new InetSocketAddress("www.google.de",80),200);
}catch(IOException e)  {
 e.printStackTrace();
}
}

I would like to remove the exception handling from methodSocket and
encapsulate it within an aspect as follows:

void methodSocket() {
socket.connect(new InetSocketAddress("www.google.de",80),20);
}

public aspect SocketAspect {

  pointcut p(): call(* java.net.Socket.connect(..));

after(): p() {
    throw new IOException();
}
}

What is wrong with this aspect? How can I achieve my aim?

Thanks in advance




----------------------------------------------------------------
Mensagem enviada usando o IMP (Internet Messaging Program).




Back to the top