Hi, the exam requires me to throw a particular exception type, say 'AnException' as defined by the interface. What is the best way to throw other checked exceptions such as an InterruptedException ? I thought of chaining/wrapping the InterruptedException in AnException, is there any other way?
//interface method public void methodA ()throws AnException
// implementation of methodA public void methodA()throws AnException, InterruptedException //!! can't do this as this is not declared in interface
Thanks. I'll have a look there, although this is not specifically an RMI problem.
I have a DBAccess interface defined in the assignment which is throwing an exception which is not the only exception I want to throw. There are only two ways that I can see to get around this. 1. Chain/wrap the exception and rethrow the it as the exception specified in the assignment or 2. throw a runtime exception (errr!)
author and jackaroo
Many candidates seem to go with creating their own subclass of RuntimeException to solve this problem.
Depending on the exception you are catching, wrapping, and rethrowing, this might be logical. There is at least one exception that you probably have to catch even though logically it should never occur in your application. If it can never happen, then if it does happen, your application state may be invalid, and you are probably safer throwing a subclassed RuntimeException.
The problems with wrapping the exception in one of the exceptions allowed in the method signatures are twofold:
What if the exception you are catching and wrapping does not really fit in the description of the wrapper exception? For example, NoSuchRecordException is just that - it is telling you that the record requested does not exist. Logically, when you get that exception, you would think that the database is still in a valid state, and that you can continue working, just with a different record. You should not expect to get this if your database is in an invalid state and you cannot continue working.
What if not all methods which have this problem of an undeclared exception do not all have the AnException that they can throw? For example, many of the methods you have been given could throw an IOException. But (in some provided interfaces) not all of them throw the same checked exceptions. This means that you have the same base exception causing different exceptions depending on the method called.
Hmmm, this proably would have been easier to read if I had used the real exception names. Oh well ...
Joined: Aug 11, 2004
thanks very much, I came to the same conclusion myself, I just thought it a little strange that Sun would give us a situation like this, I'm not quite sure what they are trying to achieve. thanks once again Steve
author and jackaroo
Originally posted by Stephen Anson: I just thought it a little strange that Sun would give us a situation like this, I'm not quite sure what they are trying to achieve.
I have heard that Sun are trying to test candidates on multiple levels. One is that they are trying to see how candidates work with imperfect specifications (such as you do get in the real world), and another is they are trying to see how candidates work with requirements that seem illogical (at least to the candidate). It is far more valuable to a potential employer if they know that the person who has been certified will work to requirements and can handle imperfect specifications, rather than employing some "superstar" who does not want to work with the team.