Dear Java Programmer:
a) The exception is now declared without constructor or other in it.
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?
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)
Questions:
1)
I did not write a toString method inside of the exception class to override the
toString method of the ArithmeticException class?
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?
Respects,
JKid314159
http://existentialists.blogspot.com/
--- On Sat, 8/1/09, miga <[email protected]> wrote:
From: miga <[email protected]>
Subject: [java programming] Re: Lab1007hw
To: "Free Java Programming Online Training Course By Sang Shin"
<[email protected]>
Date: Saturday, August 1, 2009, 4:16 AM
On Aug 1, 1:53 am, JKid314159 <[email protected]> wrote:
> Dear Java Programmer:
>
> //This iexception throws non-negative value
> class MyOwnNegativeValueEnteredException extends ArithmeticException {
> public MyOwnNegativeValueEnteredException(){
> ArithmeticException();
> }//constructor
>
> }//
You do not need any constructor, it will call automatically the super
constructor as you extend it. Now you need perhaps to overwrite its
toString method to return the message you want to be returned when
hitting the exception. If you recall well, we have already used the
toString method in another lab (inheritance or so).
Then you may use this exception for the size by throwing it if size is
negative, idem for the zero value exception.You may also used it in a
JOptionPane. Just remember that exceptions are caught in a logical
order, so don't try to catch any arythmetic exception unless you are
already sure the number passed is really a number.
> I am trying to call the ArithmeticException and make it output a string such
> as "Non-negative value."
So, put it in the toString method.
>
> I just can't grab ArithmeticException in a custom way.
Next, for example, you may throw it for size if size is negative. Use
a try/catch block for this.
And you may catch it an use it in a JOptionPanev show message dialog
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---