aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Consuming IOExceptions in database layer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Consuming IOExceptions in database layer" Watch "Consuming IOExceptions in database layer" New topic
Author

Consuming IOExceptions in database layer

Tadas Subonis
Greenhorn

Joined: Aug 05, 2011
Posts: 8

Hello, champs,

is it ok to consume messages like this:



or like this:



I have doubts that in some cases it would be better to propagate exception with


but I am not quite sure how examiners "react" to such things...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5596
    
  15

Logging an exception is of course better than just swallowing (ignoring) it and doing nothing at all. But I think just logging them is just not enough. If you can't handle them appropriately in the Data class you should propagate them to the next layer. You suggest using a simple RuntimeException, but it might be better to use a specific (custom created) DBException (which is a runtime exception). So you can catch it in the next layer and handle appropriately.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Tadas Subonis
Greenhorn

Joined: Aug 05, 2011
Posts: 8

So I guess it is allowed to use more exceptions than it is provided by Oracle DB interface (in my task there isn't said anything about creating my own new exceptions) ?
Tadas Subonis
Greenhorn

Joined: Aug 05, 2011
Posts: 8

One more thing... I quite dislike checked exceptions and more fond of sub-classing RuntimeException (read here summary). How Oracle/Sun looks at the use of RuntimeException sub-classes instead of checked exceptions? Won't it be considered "bad practice" (still I would add @throws in javadoc...) or should I explain in choises.txt why I chose subclassing RuntimeExceptions and all will be ok? Also there is this paper which in summary says that I should not use unchecked exception in my code (but in this specific case, that I mentioned in first post, it seems to be ok to propagate RuntimeException) . I am confused :/
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5596
    
  15

Tadas Subonis wrote:So I guess it is allowed to use more exceptions than it is provided by Oracle DB interface (in my task there isn't said anything about creating my own new exceptions) ?

Yes, you can (and you can add them to any package you want)
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5596
    
  15

Tadas Subonis wrote:or should I explain in choises.txt why I chose subclassing RuntimeExceptions and all will be ok?

I think the explanation writes itself (and is completely useless). If you throw a checked exception (like IOException) from e.g. the read-method, your code would not compile, because this exception is not declared in the given interface. So you can only use runtime exceptions in the Data class to throw (besides RecordNotFoundException).

I think you should carefully choose if you opt for a checked exception or a runtime one. If you do it carefully, exception handling will be a very powerful (and useful) asset. But if you do it wrong, it will result in a code mishmash (hard to maintain, extend,...)
Dmitri Cherkas
Ranch Hand

Joined: Mar 22, 2010
Posts: 40
Roel, if you have DBException or, for example, InvalidDBException as unchecked (Runtime) exceptions how do you inform the user that problems reading file occur (in my case InvalidDBException throw if database file does't exist or magic cookie value is wrong and these situations hit to recoverable conditions (therefore checked exceptions)) ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5596
    
  15

By catching and handling the InvalidDBException appropriately.

For example when user enters/selects the database location (to start the standalone client), I try to load the records into my cache. If that's not working because magic cookie is wrong, the user gets an information dialog and then application exits.
Tadas Subonis
Greenhorn

Joined: Aug 05, 2011
Posts: 8

So in summary: I shouldn't fail exam if I choose (and in other parts of project) RuntimeException in favor of checked ones?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5596
    
  15

Certainly not! You maybe could lose some points, because not using it consistently throughout your application or you used an unchecked exception while the situation required a checked one. But that's just a wild guess, because I'm not an accessor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Consuming IOExceptions in database layer