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

Can I add exceptions to an interface?

 
Steve Knoll
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SCJD assignment specifies an exact Java interface that I am to implement. However, they specify too few exceptions. For example, one method takes a String[], and the method throws no exceptions! IMHO, there should be an exception to signal that the caller did not pass thru valid info.

My question is: should I add the exceptions to the interface, and document my decisions, or simply hide the problems (if possible) to obey the instructions?

[I'm hoping you folk can help out here, so I don't have to bother Sun.]

Thanks
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot change the interface.
See the following suggestion in order to get round this problem.
http://www.coderanch.com/t/186319/java-developer-SCJD/certification/Exception-handling-IOException-not-DB
 
Steve Knoll
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, James.

So the answer is: do not change the sacred interface.

I have two follow-on/related questions:
a. Would it be better to declare unchecked exceptions that extend RuntimeException, or Error? I'm guessing RuntimeException. I'll throw these new unchecked exceptions, to leave the specified interface untouched.

b. Would the graders prefer that I create my own checked SecurityException, or use the unchecked java.lang.SecurityException? I prefer checked exceptions, but with regards to the SCJD, I must consider the graders' opinions.
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Knoll:
Thanks, James.

So the answer is: do not change the sacred interface.

I have two follow-on/related questions:
a. Would it be better to declare unchecked exceptions that extend RuntimeException, or Error? I'm guessing RuntimeException. I'll throw these new unchecked exceptions, to leave the specified interface untouched.

b. Would the graders prefer that I create my own checked SecurityException, or use the unchecked java.lang.SecurityException? I prefer checked exceptions, but with regards to the SCJD, I must consider the graders' opinions.


I know nothing of the grader's opinion, but I defined a DataException that extends RuntimeException to chain the IOExceptions and other problems that occur in the database file i/o. I also defined a checked SecurityException.
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,


...but I defined a DataException that extends RuntimeException to chain the IOExceptions and other problems that occur in the database file i/o.


Are you saying that you catch an IOException then re-throw a custom RuntimeException like so..?



What about when the function signature is as follows:



Do you then catch a generic Exception and re-throw an IOException?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic