File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes IOException in Data idea, pls comment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "IOException in Data idea, pls comment" Watch "IOException in Data idea, pls comment" New topic
Author

IOException in Data idea, pls comment

Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
I'v read some thread about exception design, now I show my juvenility idea, please comment it.

I create one unchecked exception DataAccessException and wrap the IOException in it, as below:



When anywhere catch the IOException, I wrap this exception in DataAccessException and throw it,
then in my logic layer will catch DataAccessException and execute getIOException method catch
the IOException and throw or deal with it.

Whether this idea is OK, please point out the hole.
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
There is a built-in way to do this. Simply extend RuntimeException like this:

Then when you catch the DataAccessException you can use the inherited getCause() method to retrieve the original Exception.
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Originally posted by Matt Sheehan.:
There is a built-in way to do this. Simply extend RuntimeException like this:

Then when you catch the DataAccessException you can use the inherited getCause() method to retrieve the original Exception.


Hey Matt, I'm not sure what assignment version you are using, but if I were to use the code example above, I would be violating an explicit must. My doc says that any exception created by us must have a default contstructor and another constructor that takes a string representing the error. Here was my code for my own exception.



And if you want to use it later in code, it would be something like:

Hope this helps!


SCJP 1.4<br />SCJD 1.4
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
Originally posted by Daniel Simpson:

Hey Matt, I'm not sure what assignment version you are using, but if I were to use the code example above, I would be violating an explicit must. My doc says that any exception created by us must have a default contstructor and another constructor that takes a string representing the error.


Quite right. I omitted those contructors from my earlier post. I actually provided four constructors for each Exception. The two you mentioned and also the other two from the Throwable class. The instructions do require the first two, but they don't forbid the others. Also my instructions (seem to) explicitly single out the Exceptions that are declared in the required interface for those requirements.
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Hi Matt:

As you say:

So when I catch it in this example, I should do so:

In the future, in my client layer, how I catch this:


It's right?
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
Hi Leo,
You may want to cast the cause-Throwable to an IOException before you rethrow it. Then you can catch it normaly in the client layer.
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Now I have three type exception:

1. Java defined exception: IOException(because read one file), PatternSyntaxException(because use regex). These exception are all wrapped by DataAccessException.

2. My defined exception:
2.a Server(DB) layer: DuplicateKeyException, RecordNotFoundException, DataAccessException, RecordHasModifiedException. These exception will be thrown at Data.class, and will be catch at logic layer.
2.b Client layer: URLyBirdException. When logic layer catch exceptions above, it will rethrow only URLyBirdException. So, at client layer, there is only one exception -- URLyBirdException. All the exception such as IOException will never be catched at client layer.

It's right??
What's your idea. Thanx.
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
Sounds OK to me.
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Hello,

Leo wrote:

2. My defined exception:
2.a Server(DB) layer: DuplicateKeyException, RecordNotFoundException, DataAccessException, RecordHasModifiedException. These exception will be thrown at Data.class, and will be catch at logic layer.

I have the B&S assignment, and I thought that the Data class could only throw DuplicateKeyException, RecordNotFoundException, and SecurityException. I don't think we can add a new exception, in your case: DataAccessException. I want to add a similar new exception, but do not know how to fit it in with the required design. Currently, my Data class, which accesses the file, is eating the IOException. I want to change this, by wrapping the IOException in a DatabaseException, but how can the new exception be thrown without violating the requirements?

Thanks for any input.

-Paula

Ed Villamizar
Ranch Hand

Joined: Sep 02, 2004
Posts: 47
Originally posted by Paula Decker:
Hello,

Leo wrote:
I have the B&S assignment, and I thought that the Data class could only throw DuplicateKeyException, RecordNotFoundException, and SecurityException. I don't think we can add a new exception, in your case: DataAccessException. I want to add a similar new exception, but do not know how to fit it in with the required design. Currently, my Data class, which accesses the file, is eating the IOException. I want to change this, by wrapping the IOException in a DatabaseException, but how can the new exception be thrown without violating the requirements?

Thanks for any input.

-Paula



Hi Paula,

I am doing exactly the same thing you are, which is handling the potentially thrown IOExceptions inside Data. I wanted to add a couple of exceptions to some of the Data methods as well, but I don't want to change anything in the provided interface.

Inside Data I am printing an informative message to the console and terminating the application by throwing a new RuntimeException constructed with a String explaining the cause as well.

I'd like to hear any other approaches.

Thanks,

Ed


MBA, PMP<br />SCJP, SCJD<br />ITIL Foundations Certified.
Ed Villamizar
Ranch Hand

Joined: Sep 02, 2004
Posts: 47
Paula,

After thinking about the way I was handling the IOExceptions in the Data class I realized that that's the answer to what we want to do: we can have Data throw our own unchecked exceptions (extended from RuntimeException) and handle more elegantly, with error message to the user and controlled exit, at the clients.

Comments? What do y'all think?

Thanks,

Ed
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Thanks, Ed. I'll plan to create a new class: DatabaseException which is derived from RuntimeException, and throw it from Data as you pointed out. Thanks for replying! -Paula
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Hello,

I have progressed a little further and now get compilation errors on each of the methods in the remote server. Here is one of the errors:

... readRecord(long) in suncertify.server.DBServerImpl cannot implement readRecord(long) in suncertify.db.DBAccess; overridden method does not throw java.rmi.RemoteException

I think this worked in Max's code because the DBAccess interface threw an IOException which is the super class of RemoteException.

How are others handling this problem? How have you solved it, Ed?

Thanks.

-Paula
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
I found the answer by reading this link:

Local Adapter Issue

-Paula
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
I ran into this exact same problem. I decided to have my DBClient interface throw an IOException in each public method, in addition to the other exceptions I've defined. The remote implementation throws RemoteException, and the local implementation throws IOException. In the GUI, I am catching IOException.

What answer did you find in the link you provided? I read it, but didn't find one that I was satisified with.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Hi Paul,

I have the B&S assignment, and my understanding is that you cannot add a throws IOException to the DBAccess interface. This will result in an automatic failure.

My understanding from the Local Adapter link, is that the server implements an interface which:
1) only extends Remote
2) contains the signatures of all methods in DBAccess (including exceptions thrown), but adds the RemoteException

If your server implements this interface, you won't have the compiler error.

-Paula
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
I actually didn't add any to the DBAccess interface. I designed my Data class implementing DBAccess, as specified in the instructions. Then I created a DBClient interface, which is an Adapter that uses the actual Data class. The DBAccess interface also contains methods more specific to the client, such as bookRecord and unBookRecord methods.

I have a DBLocalAdapter class that implements DBClient, and this is used as the local adapter. I also have a DBRemoteAdapter, which implements DBClient and Remote, and this is my remote adapter. The DBConnector class will return the appropriate DBClient object based on the flag.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
To add on to my last post, by creating an Adapter interface (my DBClient), it makes it very easy for me to design the application to work in either local or networking mode. When the application starts, based on the command line arguements, it will call either a getLocalAdapter() or getRemoteAdapter() from the DBConnector class. The return type is DBClient - a DBLocalAdpater for getLocalAdapter() and a DBRemoteAdapter for getRemoteAdapter(). From that point on, all of the code is the same. The method calls are the same regardless of the adapter implementation.
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Paul,
I have a DBLocalAdapter class that implements DBClient, and this is used as the local adapter. I also have a DBRemoteAdapter, which implements DBClient and Remote, and this is my remote adapter. The DBConnector class will return the appropriate DBClient object based on the flag.

Do you have trouble compiling DBRemoteAdapter?

I followed part of the design from Peter Wooster in the Local Adapter Issue.
Here's the result:

1) Create an interface which extends only Remote and has all method signatures of DBAccess, but adds RemoteException.
2) The server implements this and contains an instance of Data.
3) On the client side, wrap the Remote Server reference in a class which implements DBAccess. DBProxy for example.
4) The client can now have a variable: DBAccess dbAccess, which represents either the local (an instance of Data), or remote (DBProxy).

-Paula
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Paul -
Please ignore the first part of my last post; I see you've already solved it by your solution.
-Paula
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Hi, Paula:

I have the B&S assignment, and I thought that the Data class could only throw DuplicateKeyException, RecordNotFoundException, and SecurityException. I don't think we can add a new exception, in your case: DataAccessException. I want to add a similar new exception, but do not know how to fit it in with the required design. Currently, my Data class, which accesses the file, is eating the IOException. I want to change this, by wrapping the IOException in a DatabaseException, but how can the new exception be thrown without violating the requirements?

Thanks for any input.


Like you said after, my DataAccessException is a unchecked exception, it will be caused and unwrapped to IOException in my logic layer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IOException in Data idea, pls comment
 
Similar Threads
throws/throw
(B&S)Handling IOExceptions et al?
nx:All of URLy Bird 1.1.3 all method in Data
Need help with Exceptions
Wrapping IOException in a typesafe way