Jump to content
xisto Community
SP Rao

Help Needed - Problem In Exception Handling

Recommended Posts

I don't know where to start. I'm stuck in a strange position. Well, I have a class in which I have three functions which use to log exceptions when they occur. I've declared them as static functions as they'll be common for all the threads. Now, I have to enhance my class. For one specific exception, I need to perform soem set of operations. That means, when that particular exception acuurs, I need to log it in a variable (Say it's of type boolean and I want it to be true when the exception occurs). Now, since I detect and log exceptions using static member function of my class, I can only access static variables of the class. but I can't make this varioable static because if I make it as static, then every thread will be shouting as though they've got the exception though in reality only one thread has encountered an exception. My design has gone too far to change it by making the exception handling function no - static (normal member function) on the other hand I can't have the indicator variable as static either.Any alternate solution possible??? Kindly throw light here...

Edited by SP Rao (see edit history)

Share this post


Link to post
Share on other sites

I don't know where to start. I'm stuck in a strange position.

Well, I have a class in which I have three functions which use to log exceptions when they occur. I've declared them as static functions as they'll be common for all the threads. Now, I have to enhance my class. For one specific exception, I need to perform soem set of operations. That means, when that particular exception acuurs, I need to log it in a variable (Say it's of type boolean and I want it to be true when the exception occurs).

Now, since I detect and log exceptions using static member function of my class, I can only access static variables of the class. but I can't make this varioable static because if I make it as static, then every thread will be shouting as though they've got the exception though in reality only one thread has encountered an exception.

 

My design has gone too far to change it by making the exception handling function no - static (normal member function) on the other hand I can't have the indicator variable as static either.

 

Any alternate solution possible??? Kindly throw light here...

 

Well, some code posted might help, but on its face, the problem you pose is impossible without changing your design. Fortunately, you may be able to change your design in steps so you can reduce the impact. I provide Psuedo-code, it will need work to compile.

 

First, add member functions to call your static methods. Test.

 

Modify your classes to call their member functions instead of the static functions. This should be just search/replace. Test.

 

Now, you have a couple of choices for handling your special class(es). If it really is a one-time change for just that class, override the member function to do what you need in just that class. Otherwise, if this new behavior is something you may want to reuse in other, unrelated classes, create a new class (Call it 'E" for the moment) just to contain the exception handling code (the three member functions and the variables that were static). Now, add a field to your classes which points to an instance of this class.

 

struct E{	void e_handler1() = 0;	void e_handler2() = 0;	void e_handler3() = 0;};

Next, modify your member functions so that they check this field. If non-null, call the exception handler-instance's members. If it is null, call the static methods.

 

class YourClass{	E* e;	e_handler1()	{		if (e != NULL)		{			 e->e_handler1();		} else {			// handle normally		}	 }};

And test.

 

Now you can specialize E to specialize the error handling on an class-by-class or even instance-by instance basis. You still have the static-like benefit that instances which share a particular E also share the same variables.

 

class my_special_class{	static E* static_special_e;	my_special_class()	{		if (static_special_e ==0) special_e = new Special_E();		e = special_e;		// or whatever	};};

To make things neater, create a Singleton of a basic E instance and have every instance get this by default. The your handler code always calls e->e_handler1(). Over time, replace all your calls to e_handler1() with calls to e->e_handler1() and deprecate the old methods.

 

Anyway, that is roughly how I would do it.

Share this post


Link to post
Share on other sites

I too do not find any other way of solving this other than the phased approach that evought has beautifully explained. I was pondering over how to do it, and came to a similar solution, and when I came to post it here, I saw that he had already done a better job.

 

Anyway, I am still not exactly sure at the design of how all the things are placed in your design architecture, but I would also suggest something in terms of the following lines:

 

As evought said, consider using a class to handle only exceptions. If you have different exception, either inherit them from the base class of exception or make if you are sure the types of exception are never going to grow, make them as one class - just an exception handling package.

 

Next, you make the three functions that you mentioned call the methods of these classes. I am still not sure if you would be allowed to do that, because then, you would need to call methods of a static instance of that class. (My memory on static/dynamic classes/methods/objects are a bit blurry - so you need to bear with me). Now, if you are able to do that, then, I do not see any other way of approaching with out any design change.

 

Edit: OK. About the rules on C++ static functions - I searched around and I got the following using Google search:

A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.

A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.

A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual

A static member function cannot have access to the 'this' pointer of the class.

Source: http://forums.xisto.com/no_longer_exists/

 

Anyway, as it says above there, static functions can acess other functions outside the class. So, if I understand your problem correctly, you should be able to call some other exception handling function that you should not restrict by making them static. Those functions can play with the data anyway they want without anyway you want. That way you should be able to track which exception is raised, and depending on that you can perform a particular action.

 

PS: I may have got this totally wrong though - you will have to tell that. If that is the case, do explain the problem in more detail. :)

Edited by Vyoma (see edit history)

Share this post


Link to post
Share on other sites

First of all thanks a lot guys for showing interest in the problem. evought beatufully described an algorithm to achieve it. Well mate, unfortunately that solution doesn't work. Simply because by putting a wrap around function for the statics function of mine, I'm effectively making my static function non static!

 

The very purpose of me defining the function static was, to enable exception handling irrespective of the object. (As we both know static functions can be called without an object. Static functions will be defined only once in the memoy. Instead of allocating memory everytime an object is defined, for static functions the compiler just reffers to the same function which is created in the memory.) Which in turn means, there is only one instance of my static function at a time. This is very essential for performance of my code. The fact that only one instance of static function is needed in turn has a lot of benefits.

 

By putting the wrap function I'm destroying it all... My architecture will anyway change because this non-static member function needs an object to be called! (Which in turn means, I've made my static member function effectively non-static).

 

That solution won't serve my purpose though extremely well crafted and very well explained by evought.

 

Anyway, as it says above there, static functions can acess other functions outside the class.

Well Vyoma, this is exactly what I used as explained below.

And as for having an exception handling class library I already have one, but the exception catching mechanism I must incorporate in m class itself. (Kindly forgive me if the words are mispelled, I'm using a pathetic keyboard here in this net cafe).

 

I guess I'm left with no option nut to change design I believe.

 

I've come up with another stratergy though. For every thread I'm gonna open, I'm storing the address in a linked list.(In fact I'm using the great RogueWave library for it :) ). Now if you can imagine, through this pointer which is stored in my list, I can actually access the "this" pointer for the object. Check below for the pseadocode.

 

Static function SF(){//code goes here	(check for the pointer to current thread in the list)	{		Current Thread Pointer->this->My variable=True;   //Cought a definite exception	 //Code goes here	}return}

As you can see in the above, I've accessed the object which caused the exception in a different way :)

With some enhancement for my code.

The problem solved, issue ends here.

 

Now if you are interested, this library RogueWave is exceptionally good for string handling ;-)

visit http://www.roguewave.com/ for details.

 

Thanks a lot guys.

Share this post


Link to post
Share on other sites

I know this is rather out of date, but for anyone else who visits this topic, I have come up with a simple solution. You just pass the static function a reference to the current object, e.g.

class obj {//...static void call_static(obj&) {/*...*/}//...call_static(*this);//...}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.