Unlimited Technique          Unlimited Wisdom

1001010000101001000010010001000010000101101110111 1101010010010101011110010101110101011010101010101

导航

Intercepting Exception Errors

 

 I studied the “jbossaop_userguide” in the morning. Finding a AOP feature about intercepting Exception Errors. I think that my AOP framework should have the same feature. I spent about three hours adding the function in the morning.

     I need two interfaces for finishing the function. They are :

(1)IMethodExceptionAdvice 

(2)IConstructorExceptionAdvice

 

The first interface will be used in Method Exception Interception. The second interface will be used in Constructor Exception Interception. To intercept the Exception, we must implement the interface. e.g.

 

    public class Aspect1 : IMethodExceptionAdvice, IConstructorExceptionAdvice

    {

        public void HandleException(Exception exception,

IMethodInvocation methodInvocation,

ref Object result, params object[] args)

        {

            //throw new Exception("I succeed!");

            result = "hello world";

        }

        public void HandleException(Exception exception,

IConstructorInvocation methodInvocation, params object[] args)

        {

            throw new Exception("I succeed!");

        }

    }

If an exception occurs in a method, then the first HandleException in the Aspect1 will be invoked. If an exception occurs in a constructor, then the second HandleException in the Aspect1 will be invoked. The result parameter in the first HandleException is the return result of method of raising the exception. When a method raises an exception, its return value is null, i.e. a return operation is not executed. e.g.

 

        public String Method()

        {

            throw new Exception("An exception occurred.");

            return "Here's return value"; // Here's a line unreachable code

        }

If using the IMethodExceptionAdvice interface the exception can be intercepted, even be cancelled. If a value is assigned to the result parameter, then the Method will return the value, otherwise, or return null, or throw a new exception(Being likely to be the original exception). Constructor is as same as method.

An introduction to the InnerException:

The InnerException is a property of Exception, it saves the prior Exception thrown. e.g.

    public class MyException : Exception

    {

        public MyException(String msg, Exception e)

            : base(msg, e)

        {

        }

}

    class TestException

    {

        public TestException()

        {

            try

            {

                throw new Exception("An Exception occurred!");

            }

            catch (Exception err)

            {

                throw new MyException("A MyException occurred!", err);

            }

        }

}

 

    public class Test

    {

        public static void main(String[] args)

        {

            try

            {

                TestException t = new TestException();

            }

            catch (MyException err)

            {

                Console.WriteLine(err.Message);

                Console.WriteLine(err.InnerException.Message);

            }

        }

    }

 

The result is :

A MyException occurred!

A Exception occurred!

 

   

posted on 2005-08-04 15:52  bughole  阅读(367)  评论(0)    收藏  举报