• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exercise 5-4: Creating an Exception

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think it is correct ?
Book->K&B->Chapter5->Page->377

Exercise 5-4: 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?


  •  
    Sheriff
    Posts: 3064
    12
    Mac IntelliJ IDE Python VI Editor Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Pretty good. There's no need to create a String object from a String literal though. Just use the literal. In other words.



    Also, it's good when doing a String comparison to make the call on the String you know isn't null. So:



    Finally, if you're trying to get a case insensitive comparison there, you're missing a lot of cases: "Bad", "BAd", "bAd", etc. Instead do:



     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much Greg Charle

    I've one doubt, whereas i know equal() takes object as argument, then how string literal is allowed here? because String class doesn't provide wrapper class for string literal
     
    Greg Charles
    Sheriff
    Posts: 3064
    12
    Mac IntelliJ IDE Python VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    String is one the very few ways where the Java language and the Java API mix. A String literal is actually an object, an instance of the String class.
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Greg Charles wrote:String is one the very few ways where the Java language and the Java API mix. A String literal is actually an object, an instance of the String class.



    Thank you.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic