[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] convert unchecked-exception to checked-exception

Hi Andy,

thank you for your "advice". It will solve the problem.
If I use:

   declare soft : PersistException: mypointcut();

The woven code will look like that:

        try {
            try {
                throw new PersistException();
            } catch (Exception exception) {
                throw exception;
        } catch (PersistException persistexception) {
            if (persistexception instanceof RuntimeException)
                throw persistexception;
                throw new SoftException(persistexception);

The "declare soft"-line will do just what i need: It avoids the unhandled-exception-error and the woven code is meaningless in this situation.
The drop of bitterness is that i have to be very carefull with this solution. Couldn't we have a Solution without the "declare soft"-line?
The "after() throwing" advice will catch the Persist-Exception. The Compiler does not need to generate an unhandled-exception-error.

Your suggested compiler-change would take in account that if someone declares a method to throw some checked exceptions,
he really wants to throw them in his code. I think nobody would expect that these exceptions will get softend.

Thank you,

2012/5/24 Andy Clement

In your particular situation you could:

 declare soft : PersistException : mypointcut();

then the DSException wouldn't get softened.

I did just prototype a compiler change that would mean the woven code
looked like this:

try {
  try {
      throw new PersistException();
  } catch(Exception exception) {
     throw exception;
} catch(Exception exception1) {
   if(exception1 instanceof RuntimeException)
      throw exception1;
   else {
   if (exception1 instanceof DSException) { // NEW CHECK, don't wrap
if OK to throw
     throw exception1;
   } else {
      throw new SoftException(exception1);

which also appears to get you what you want.  The DSException is not
wrapped because it is declared to be thrown at the joinpoint.  This
feels like a variation of declare soft though (so maybe a syntax
variation to request this behaviour) - and I'm not sure how often
users hit this scenario. Is the ability to control what is softened
through the declare soft type pattern sufficient for you?


On 24 May 2012 02:06, Straub Oliver wrote:
> Hello,
> there has been long discussions on checked vs. unchecked exceptions.
> I don't want to start this discussion again.
> I know, the opposite behaviour can be achieved by "declare soft ..."
> In my case i have to convert any exception to a layer-specific checked
> exception.
> This behaviour shall be controllable (on/off) with the presence of a
> method-annotation.
> Here an example:
>   @DSExceptionTranslator
>   private void anyMethod() throws DSException {
>       throw new PersistException();
>   }
> Obvious the above code will normally generate an unhandled-exception-error.
> I expected the following advice to solve that.
>   after() throwing (Exception ex) throws DSException : mypointcut() {
>       throw new DSException(ex);
>   }
> But the unhandled-exception-error did not disappear!
> So i added a "declare soft":
>   declare soft : Exception : mypointcut();
> The unhandled-exception-error disappears, but this did not solve my problem
> because the generated code looks like this:
> try {
>    try {
>        throw new PersistException();
>    } catch(Exception exception) {
> ThrowDSExceptionAspect.aspectOf().ajc$afterThrowing$com_gid_bip_dataservice_aj_ThrowDSExceptionAspect$1$e66f2abe(exception);
>       throw exception;
>    }
> } catch(Exception exception1) {
>     if(exception1 instanceof RuntimeException)
>        throw exception1;
>     else
>        throw new SoftException(exception1);
> }
> As we can see, my new DSException is softened too and is thrown as a
> SoftException.
> It seems that the code for "declare soft" will always be weaved as last
> step.
> I tried "declare precedence" to control the weaving-process, but nothing
> changes!
> ==== Is there any other way to avoid the unhandled-exception-error? ====
> I found similar Questions in several lists, but the solutions all did not
> work with the current release of AspectJ 1.6.12.M1 (ajdt 2.1.3).
> Thank you in advance
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
aspectj-users mailing list