• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Page 377 Creating an Exception Exercise 5-4

 
Greenhorn
Posts: 6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



Thanks.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very illuminating. Thank you for sharing.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic