Forwarding post: and answer in it
aa)At first I converted the integer back to a String. But now I
understand the toString to override the message. So toString will
return a message String. To do this, I write the method inside of the
SpecificExceptionCreated class.
I make the call in the catch block as such:
SpecificExceptionCreated.toString();
Error message says can not call non-static method toString from static
class?
Here is the code:
class SpecificExceptionCreated extends ArithmeticException{
public String toString{
String stringTemp = "message here";
return stringTemp;
}//end toString()
}//end SpecificExceptionCreated
You don't need a temporary string, just write:
class SpecificExceptionCreated extends ArithmeticException{
public String toString{
return "message here";
}//end toString()
}//end SpecificExceptionCreated
If you read the throwable API, the message normally returns the name
of the class and a particular message, so you may want to include the
name of the class inside your message.
maybe I have to put in separate file and make static call?
No, it is good as is.
------------------------------------------------------------------------------------------------------------------
>
> 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
------------------------------------------------------------------------------------------------------------------
bb) Suppose to run program without assertions?
------------------------------------------------------------------------------------------------------------------
Yes, if you want. As it was not specified in the homework, I let it
in, and then, if I use assertion as command line option it works,
catching correctly the assertion error, if no assertion is using as
command line option, it ignores purely the code.
------------------------------------------------------------------------------------------------------------------
cc) I created two separate exception classes, one for negative numbers
and the other for numbers equal to zero. This results in two
conditions in the if/else block which throws to the catch statement on
that particular exception.
Yes, exactly.
So each exception will have toString() method with each message?
Yes, they are completely separated. You throw an exception a1
somewhere, and it is catched in the throw b1 block somewhere else.
When you throw an exception a2 somewhere, it is in turn catched in the
throw b2 block.
Send message back to catch block and put into pane or out.
Yes, when thrown you use whichever commandline (out) or Gui
(JOptionPane) to print the message.
------------------------------------------------------------------------------------------------------------------
Please post to the group if you want an answer from me. I don't answer
personnally. And besides, I have big trouble forwarding any message.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---