On Aug 2, 2:06 am, JKid314159 <[email protected]> wrote:
> Dear Java Programmer:
>
> a) The exception is now declared without constructor or other in it.
If you don't put anything in the exception, then is it reasonable to
extend the ArithmeticException?
At least you may want to override the message returned by the
exception, this is where the toString method is useful.
>
> b) I have caught the number format exception when the value entered is not a
> number.
> I placed the parse inside of try block so format will throw runtime exception.
> Then used if/else block for conditions of negative and zero?
This is the step where you put the size inside a try/catch block in
order to catch your own negative exception
In the try block:
1 - you define the size
2 - you assert it positive
3 - you test if negative and throw your negative exception inside the
if block
In the catch block:
1 - you catch the number format exception
2 - you catch the assertion error
3 - you catch the negative exception
In the catch block, you may use a JOptionPane to show the exception.
An exception message is given by ex.toString() if you override
toString method when defining the exception, and named your exception
ex in the catch block.
> c) To override the message I put in a message of my own in the JOption
> message when I catch the exception.
>
> catch (HomeGrownException e) {
> JOptionPane.showMessageDialog(null,"message override placed
> here?");}//catch(end)
You may do this, but there is apparently not reason to do so, as if
you define an exception ,that may be to get a given message, not a
customized every time you have to use it?
What would be better is to use in the JOptionPane partly a message of
your own, say for example "size should have" and part message from the
exception "non-negative value". Though personnally I'd prefered
"positive value", but that's just me here.
>
> 2)
> Why do I have to extend the exception class to ArithmeticException class,
> since I am throwing on my condition of negative number and catching with my
> own custom message?
Yes, the way you do it, it has no sense to extend ArithmeticException,
but that's not what is asked in the homework, and unless you have good
reason to do so (which frankly I don't see here), it is better to
write a precise Exception and use it afterwards in the program;
following the principle of don't repeat yourself.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---