aspose file tools*
The moose likes Cattle Drive and the fly likes OOP-2 : NaturalLanguageMultiply Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "OOP-2 : NaturalLanguageMultiply" Watch "OOP-2 : NaturalLanguageMultiply" New topic
Author

OOP-2 : NaturalLanguageMultiply

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Marilyn, you gave me a comment to try not to throw a new exception if I can do it without one. I can write methods that return something like a -1 or something for errors so as to avoid throwing exceptions, but I thought the assignment specfically stated for us to throw an exception.
Just wondering which way I should go.
Bill
PS Sorry about the spacing agina
Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
Hello Bill,
I'm guessing that Marilyn might not want you to create a new type of exception and use that, if that's the way you went. I just submitted my first version of this assignment myself and your question got me thinking about the new type of exception that I used, and whether or not it was really needed. I went back to the program and was able to trim quite a few lines of code by just using the existing "Exception" class. I'm expecting the nit-picker to hammer me for my potentially unnecessary class, and I'll be ready with version two.

Pat B.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Throw an exception with text for the user to see in one method.� Have your main method catch the exception and show it to the user. You don't need to throw a new exception in each method.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
OK, I resubmitted. I kind of feel the code I had last time was cleaner, but I was having trouble finding a way to not throw a new exception if I broke things out into two methods, one that takes a String and returns the int (toInt method) and one that checks the values for hyphens, breaks the numbers apart, and makes sure there isn't something like two-two or twenty-thirty.
So I wrapped everything into the toInt method so I would only throw one exception. My other choice was not to have the toInt method throw an exception but have the calling method (the one that process the numbers and breaks them apart if there is a hyphen and checks for odd things like two-two) throw the exception. But that goes against the rules of the assignment as the signature for the toInt method is given.

Bill
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
I was confused before I read these posts, now I'm even more confused...

I am working on OOP-2 myself. I have three methods. toInt() is the middle one. Marilyn is asking me to throw the exception in the convert() method and let it "bubble up" to main.

Anyone have any suggestions? According to my cert exam results, OOP and exceptions were the two weakest areas!


Marcus L�ubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Marcus,

When an exception is being thrown from a method, there are two ways for a calling method to handle it:
1. catch and handle the exception using try/catch statements
2. ignore the exception and let the next calling method to handle it.

Here is an example:


For the OOP-2 assignment, what Marilyn suggests is to propagate or "bubble up" the exception to the main method to let it handles, just like methodA.

Other resources:
The book "Robust Java: Exception Handling, Testing and Debugging" by Stephen Stelting provides some tips and advice on handling and designing exception. There is a tutorial on Exception by Sun.

As for OO, I suggest you read some of the design principle articles at objectmentor.com, very useful for OO design.

Joyce
[ April 12, 2005: Message edited by: Joyce Lee ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Originally posted by Pat Barrett:
Hello Bill,

I'm guessing that Marilyn might not want you to create a new type of exception and use that, if that's the way you went. I just submitted my first version of this assignment myself and your question got me thinking about the new type of exception that I used, and whether or not it was really needed. I went back to the program and was able to trim quite a few lines of code by just using the existing "Exception" class. I'm expecting the nit-picker to hammer me for my potentially unnecessary class, and I'll be ready with version two.

Pat B.


Although throwing generic Exception is quick, easy, and lowers the class count, I usually try to wrap it.

First, because I can make it more sensitive to the particular issues surrounding the exception.

Secondly, because it's easier when you're calling some sort of generalized service to be able to add in specialized logic to deal with exceptions when you can apply the instanceof operator to an exception (assuming you coded a general Exception catch) than it is to dissect the Exception message and go from there. Especially if it's an I18N message.

Exception rethrowing is often useful as well. I've done things like written parsers for reading lines in an InputStream where a bad field is tagged and thrown in an exception, then the line-processing loop catches it, adds the line number to the exception and throws it up to the caller.

Another favorite trick is chaining Exceptions when I'm constrained because I'm being called by someone whose idea of what can go wrong is limited. For example, when I'm doing callbacks for a SAX parser. They may want a SAX exception to be thrown. So I construct a SAX exception chained to the real exception. That way, we're both happy. And unlike an unchecked exception, the SAX processor isn't bypassed on the way back up, so proper SAX hygiene is preserved.
[ April 13, 2005: Message edited by: Tim Holloway ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Although Tim has brought up some interesting ideas, in this case we are looking for the simplest thing that could possibly work. I think using the existing Exception class will be adequate for our purposes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OOP-2 : NaturalLanguageMultiply