• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception problem is bugging me!

 
Vikas Sood
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
I have the following doubt regarding exception handling:
My DataService interface which my Data class is implementing has the lock method throwing IOException.I am not having this method throw RemoteException as it is a subclass of IOException.
Then My LockManager class which is where locking is actually implemented has its lock method throwing InterruptedException.
As the locking is used in Network mode only, my RemoteData class (adapts Data for remote database services ) which is implementing RemoteDataservice (remote version of DataService) and makes use of the LockManager for locking, inside its own lock method catches the interruptedException thrown by LockMangers lock() and rethrows it in form of IOException which is caught in Model class and rethrown as DatabaseException to Controller which handles it and displays a a user frindly message shown to the client by means of a dialog.
So based on the above scenario there are a couple q's which come to mind:
1)Shouldnt i also be throwing RemoteException in lock(),handling is seperatly from IOExceptio, which is what i am doing for all other methods throwing RemoteException?
2)The conversion of InterruptedException into IOException in RemoteData's lock() and then the conversion of this IOException into DatabaseException in Model ,and final handling of this Exception in Controller,is this an proper way of how to do it?
3)If not then, how can the above be achived?
Suggest some approaches, as i am getting stuck at this issue now.
VikasSood
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikas
1)Shouldnt i also be throwing RemoteException in lock(),handling is seperatly from IOExceptio, which is what i am doing for all other methods throwing RemoteException?

I don't quite understand where this lock() method is. If it is on the client side, then it depends on what you do when you catch the IOException there. If you treat this as a fatal error then there is no need to handle the RemoteException differently. If you are treating the IOException as something to warn the user about, but otherwise ignoring the exception, then you probably do need to handle the RemoteException differently as they no longer have a valid connection to the server.
2)The conversion of InterruptedException into IOException in RemoteData's lock() and then the conversion of this IOException into DatabaseException in Model ,and final handling of this Exception in Controller,is this an proper way of how to do it?

Nothing wrong with this as far as I am concerned. Normally you want to handle the exception as early as possible - the further up the chain it goes, the harder it is to recover gracefully. However at the top level, you probably do not want to have 20 different catch clauses, each doing an identical thing (such as displaying a dialog stating that an exception with message ... occurred), so wrapping the exception in the manner you are doing makes it much easier to catch only one exception later.
This is also good for extensibility. You might want to keep the throwing / catching of the RemoteException close to the class that actually does the connection. If you pass it all the way up to the Controller, and later the connectivity is changed to another protocol (say sockets, or MQ) then the RemoteException handling in the controller is going to be redundant. How many classes do you want to change in order to make what should be a simple network change?
Regards, Andrew
 
Vikas Sood
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Andrew ,
you wrote:
I don't quite understand where this lock() method is.

Iam having lock() method in my DataClient class which implements the same interface as Data.When i catch an exception finally in controller ,i create a message dialog and display it to the client, explaining the error.So i think i will have to change the lock() signature so as it also throws RemoteException apart from IOException.But i will be facing problems in handling this exception.LEts see the code below:

from the above code of book method in my Model class you can make out that throwing of RemoteException by lock() method becomes redundant, what is the way out here?
Another thing my controller is having two catch clauses,one for DatabaseException and another for RemoteException, why i am having two catch clauses,the reason for that is that i want my client to be able to know what really went wrong.Was it a remote access error or something to do with database access.
And as for future enhancement and changes in network protocol, i thinki will still like the client to be able to know waht went wrong, irrespective of what we use to connect to the network database.

Waiting for your valuable comments.
VikasSood
[ June 11, 2003: Message edited by: Vikas Sood ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikas
throwing of RemoteException by lock() method becomes redundant, what is the way out here?

What would you like the way out to be?
If you want to throw RemoteException so that your controller can catch it, then you will have to catch it in your lock method and then rethrow it. This is easy enough. Or are you asking how to do this?
Regards, Andrew
 
Vikas Sood
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,
You wrote:
If you want to throw RemoteException so that your controller can catch it, then you will have to catch it in your lock method and then rethrow it. This is easy enough. Or are you asking how to do this?

The method signature which i posted above is my bookseats methods in Model class.
My controller is having two catch clauses for catching RemoteException and DatabaseException, i have caught the IOException which can occure in lock method and have rethrown it as Database exception to controller fine.
But what i wanted to ask you was,is that RemoteExceptions thrown in case of some remote error while trying to lock will be invariantly caught by my catch caluse for IOException in Model class and it is not possible to invoke the RemoteException catch clause this wayand thus i will not be able to inform client about the remote access problem.
Also do let me know your opinion on my explanation of informing client of remote errors .
Thanks & Regards
VikasSood
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikas
I am still confused.
RemoteExceptions thrown in case of some remote error while trying to lock will be invariantly caught by my catch caluse for IOException in Model class and it is not possible to invoke the RemoteException catch clause this way

I am reading this as "because I have a catch for IOException, I cannot catch Remote Exception".
Is this what you are saying?
If so, then you can catch the RemoteException. You just need to put the RemoteException catch statement before the IOException.
This will work:
Whereas the compile will fail with an error about unreachable code with this code:
Also do let me know your opinion on my explanation of informing client of remote errors .

I don't have a problem with it. In fact, I think it is a nice idea to be able to give the user a reasonably detailed error message which gives them a real idea of what might be wrong / what might fix it, rather than a generic "an error has occured".
Since I used JDK 1.4, I chained my exceptions, so even when I was receiving a DatabaseException, I could look at the cause and find out what the Exception was that had been wrapped in the DatabaseException. This meant that the signatures of some methods could be reduced down to just a single Exception.
Regards, Andrew
 
Vikas Sood
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,
thanks for your help , the problem i was having was a confusion in my mind, which caused me to beleive that an exception thrown from indside a catch clause might get caught by another catch clause.
but when i ran a code example for this, my doubts are cleared out.
VikasSood
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic