[Bluej-discuss] Exceptions
Luis Moreira
Luis.Moreira at jet.uk
Fri Jul 7 08:25:02 BST 2006
Hi Guys,
I found the solution for my exception problem, instead of catching the
exception in changeDetails I just add the throws declaration on the
method's header and catch it on the calling method.
Best regards
Luis
-----Original Message-----
From: bluej-discuss-bounces at bluej.org
[mailto:bluej-discuss-bounces at bluej.org] On Behalf Of Luis Moreira
Sent: 06 July 2006 09:31
To: General discussion for users of BlueJ
Subject: Re: [Bluej-discuss] Exceptions
Hello all,
I am still having problems with Exceptions...
I can (I think) understand now the difference between unchecked and
checked Exceptions, thanks David, the problem now is implementation.
I started by doing exercise 12.26 in page 362 and modified the method
removeDetails to throw the exception if the key is not in use:
public void removeDetails(String key)throws NoMatchingDetailsException
{
if(key == null){
throw new IllegalArgumentException("Null key passed to
removeDetails.");
}
if(!keyInUse(key)) {
throw new NoMatchingDetailsException(key);
}
else
{
ContactDetails details =
(ContactDetails) book.get(key);
book.remove(details.getName());
book.remove(details.getPhone());
numberOfEntries--;
}
}
Then I added a handler in the remove method to handle the exception,
this works fine. But when compiling because the changeDetails method
also uses the removeDetails method I also had to add a new handler to
this method. This was ok and even made sense.
The problem arises in exercise 12.27, I enhanced the user interface to
allow for change of details but the problem is since I already catch
that exception in changeDetails method in Address Book how do I change
that to catch it instead in the change method in AddressBookInterface?
Is it just the case of not providing one in changeDetails and hence the
search for the handler will move to the change method?
I read both the book and the Sun's Tutorial about exceptions and I think
I get them, I understand the mechanism to catch them but the
implementation in practice seems a bit more difficult. Am I missing
something?
Thanks for the help
Best regards
Luis
-----Original Message-----
From: bluej-discuss-bounces at bluej.org
[mailto:bluej-discuss-bounces at bluej.org] On Behalf Of David J. Barnes
Sent: 05 July 2006 09:29
To: bluej-discuss at bluej.org
Subject: Re: [Bluej-discuss] Exceptions
Hi Luis,
Actually, it is the other way around.
SecurityException is unchecked because it has RuntimeException as a
superclass.
NoSuchMethodException is checked - Exception is its immediate
superclass.
It is worth bearing in mind that there are no absolute right and wrong
rules as to which exceptions should be checked and which unchecked. One
possible reading of the reasons behind those particular exceptions is:
+ A SecurityException represents a violation of an external constraint
which is likely either to have been unanticipated or is irrecoverable -
programs should not normally attempt illegal operations. So program
failure is the most appropriate action.
+ NoSuchMethodException can result from a simple search for a matching
method during reflection in a getMethod call to a Class object. As the
program is not attempting to call the method at this stage, it doesn't
represent a fundamental failure, so the lack of a match could be
anticipated and possibly recovered from.
However, knowing that a method might throw a SecurityException means
that you can still program around complete failure by using an exception
handler if you wish.
Sometimes the decision to make an exception either checked or unchecked
is simply a compromise between programmer convenience (avoiding lots of
handlers where failure is unlikely) and robustness (complete program
failure would be unacceptable in some contexts).
Hope that helps,
David
On Tue, 4 Jul 2006 18:24:01 +0100
Luis.Moreira at jet.uk (Luis Moreira) wrote:
> Hi Guys,
>
> I am having a few problems with understanding Exceptions...
>
> On exercise 12.23 if I understood correctly SecurityException is a
> checked Exception because is not an irrecoverable error related to the
> program (I am assuming from the definition of it that this is related
to
> a password issue?). The NoSuchMethodException is an Unchecked
Exception
> because if the method does not exist them the program should terminate
> as processing is compromised. Is this correct or did I got this wrong?
>
> The other issue is, why would I need an Exception
NoSuchMethodException,
> wouldn't the compiler at compile time pick this up and flag it?
>
> Best regards
>
> Luis
>
>
>
>
>
>
>
_______________________________________________
mailing list bluej-discuss at bluej.org
To unsubscribe or change your preferences, go to
http://lists.bluej.org/mailman/listinfo/bluej-discuss
_______________________________________________
mailing list bluej-discuss at bluej.org
To unsubscribe or change your preferences, go to
http://lists.bluej.org/mailman/listinfo/bluej-discuss
More information about the bluej-discuss
mailing list