Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I know this has been dealt with before but I just want to make sure that what I am thinking is correct.

I think its best to use an example.
In my Data class I have the methods read and find with signatures as follows:

public String[] read(int recNo) throws RecordNotFoundException
public int[] find(String[] criteria)

read can encounter the following exceptions:
* EOFException
* UnsupportedEncodingException
* IOException

find can encounter the folling exception:
* IOException


In the case of read the EOFException I throw a RecordNotFoundException
With the other two I'm not sure.
I could throw a RecordNotFoundException or a DataException (extends RuntimeException)
You could make an argument for both I guess. Thoughts ?

I also throw a DataException in the case of find.
Find is obviously slightly different as it throws no Exceptions so I am pretty much forced to throw a RuntimeException.

The error messages I throw I am less sure of.
In the case of an IOException do I want to say
"IOException while trying to read record X".

Obviously this is not the kind of error message we want a user to see.
The Controller class is the only one going to be accessing the Data layer so it can return error messages to the UI that make sense.
But what level of detail does the controller care about ?
Does it want to know it was an IOException that occured or is the fact that an Exception occured at all enough for it ?

If it just cares that an Exception occured then do I need to catch IOException and UnsupportedEncodingException seperately or will catching Exception suffice ?


Thanks.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alan,

I think that throwing a RecordNotFoundException in case of IOException is a bad idea : such an IOException may occur in create, find, read, etc...

IOException and UnsupportedEncodingException are severe exceptions for the application and should not occur. It sounds resonnable to wrap them in a RuntimeException.

Regarding your controller, I think it has to deal only with those scenaris :
- Db call returns normally
- Checked exception thrown (RecordNotFound, maybe you'll want to do someting special)
- RuntimeException <-> DB severe problem "Please contact your server administrator "



If it just cares that an Exception occured then do I need to catch IOException and UnsupportedEncodingException seperately or will catching Exception suffice ?


In the data layer at least, you should catch it separatly and log them in order to find why your database is not responding...
 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Seb,

Thanks for the reply.
I only thought about the logging of the exception after I had posted the message.
Of course it makes sense to catch them seperately for that purpose.

I think that the three sceanrios you mentioned (normal, checked exception, runtime exception) make sense.

So I reckon I'm on the right track anyway
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic