[aspectc-user] Dealing with aspects

Olaf Spinczyk Olaf.Spinczyk at informatik.uni-erlangen.de
Wed Sep 7 09:34:55 CEST 2005


Hi,

the problem is quite simple:

replace ...

throw new ContractException(...)

with

throw ContractException(...)

in your advice code. Otherwise the thrown object will be of type 
"ContractException*", which is not matched by your catch statement. BTW, 
you could also weave the catch with an aspect by using around advice 
(e.g. for main()) and a try/catch block that encapsulates the 
tjp->proceed().

-Olaf


Sergio Queiroz wrote:
> Hi!
> 
> I am not used to deal with exceptions and I am trying to define an aspect that
> will verify some condition and will throw an exception. Currently, I can throw
> an exception but I can not deal with it, the program is aborted. I wrote the
> same code without aspects and it worked nice (the program finished normally),
> so I would like to know if I am doing something wrong. I saw some code from
> the AOSD tutorial but I did not notice much diference. I am using the version
> 0.92.
> 
> Sérgio
> 
> 
> 
> ------------------------------------------------------------------------
> 
> #include "Customer.h"
> 
> Customer::Customer () {
> 	
> }
> 
> int
> Customer::add (int a, int b) {
> 	return a + b;	
> }
> 
> 
> ------------------------------------------------------------------------
> 
> #ifndef CUSTOMER_H
> #define CUSTOMER_H
> 
> class Customer {
> 	public:
> 		Customer ();
> 		int add (int, int);
> };
> 
> #endif // CUSTOMER_H
> 
> 
> ------------------------------------------------------------------------
> 
> #ifndef TYPES_H
> #define TYPES_H
> 
> enum ExceptionType {PRECONDITION, POSTCONDITION, INVARIANT}; 
> 
> #endif //TYPES_H
> 
> 
> ------------------------------------------------------------------------
> 
> #include "Customer.h"
> #include "ContractException.h"
> 
> int
> main () {
> 	Customer c;
> 	
> 	try {
> 		
> 		c.add (-1, 0);
> 	
> 		c.add (2, 100);
> 		
> 	} 
> 	catch (ContractException e) {
> 		cout << "Managing the exception" << endl;
> 		e.print ();
> 	}
> 	
> 	return 0;
> }
> 
> 
> ------------------------------------------------------------------------
> 
> #ifndef CONTRACTEXCEPTION_H
> #define CONTRACTEXCEPTION_H
> 
> #include <string>
> #include <iostream>
> 
> #include "Types.h"
> 
> using namespace std;
> 
> class ContractException {
> 	public:
> 
> 	ContractException () {
> 	}
> 	
> 	ContractException (ExceptionType e, string s) {
> 		if (e == PRECONDITION) cout << "PreCondition" << endl;
> 		else if (e == POSTCONDITION) cout << "PostCondition" << endl;
> 		else if (e == INVARIANT) cout << "Invariant" << endl;
> 		cout << s << endl;
> 	}
> 
> 	void print () {
> 		cout << "Printing" << endl;	
> 	}
> };
> 
> #endif //CONTRACTEXCEPTION_H
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> aspectc-user mailing list
> aspectc-user at aspectc.org
> http://www.aspectc.org/mailman/listinfo/aspectc-user




More information about the aspectc-user mailing list