• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception handling problems

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

i.e

//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 in advance
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,

Welcome to JavaRanch and this forum.

Take a look at the "RMI implementation" topic (and join in). You may find answers there.

Regards, Andrew
 
Stephen Anson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!)
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,

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 ...

    Regards, Andrew
     
    Stephen Anson
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Steve,

    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.

    Regards, Andrew
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic