Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Page 377 Creating an Exception Exercise 5-4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Page 377 Creating an Exception Exercise 5-4" Watch "Page 377 Creating an Exception Exercise 5-4" New topic

Page 377 Creating an Exception Exercise 5-4

Saruwatari Tadayoshi

Joined: Jun 22, 2010
Posts: 6
EXERCISE 5-4 From SCJP Study Guide by Kathy Sierra & Bert Bates

Creating an Exception
In this exercise we attempt to create a custom exception. We won't put in any new
methods (it will have only those inherited from Exception), and because it extends
Exception, the compiler considers it a checked exception. The goal of the program
is to determine whether a command-line argument, representing a particular food
(as a string), is considered bad or OK.

* Let's first create our exception. We will call it BadFoodException. This
exception will be thrown when a bad food is encountered.

* Create an enclosing class called MyException and a main() method,
which will remain empty for now.

* Create a method called checkFood(). It takes a String argument and
throws our exception if it doesn't like the food it was given. Otherwise, it
tells us it likes the food. You can add any foods you aren't particularly fond of
to the list.

* Now in the main() method, you'll get the command-line argument out of
the String array, and then pass that String on to the checkFood() method.
Because it's a checked exception, the checkFood() method must declare
it, and the main() method must handle it (using a try/catch). Do not have
main() declare the exception, because if main() ducks the exception, who
else is back there to catch it?

* As nifty as exception handling is, it's still up to the developer to make
proper use of it. Exception handling makes organizing our code and signaling
problems easy, but the exception handlers still have to be written. You'll find
that even the most complex situations can be handled, and your code will be
reusable, readable, and maintainable.

Is my following solution correct?

Ankit Garg

Joined: Aug 03, 2008
Posts: 9465


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Kurt Zhu

Joined: Aug 25, 2011
Posts: 8
very illuminating. Thank you for sharing.
Alex Hadn

Joined: Dec 07, 2011
Posts: 1

i know this topic is a few month old, but i am doing at the moment the OCPJP and so i found this example.
Maybe i am wrong, but this example is not correct?
Because this way it interrupt at the moment a exception is catched and doesnt handle the rest of the commandline arguments!

should the try/catch block not be placed in the for loop instead of outside to handle all of the commandline-args??

for me it worked this way as expected!

Thanks anyway, nice done example
I agree. Here's the link:
subject: Page 377 Creating an Exception Exercise 5-4
It's not a secret anymore!