Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Exception Problems

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,
I am in the process of making some programs and decided to throw some customised error and forced to catch my errors as they have been thrown in my various classes as given belown. e.g. testException
But surprised to find that when I tried CheckInteger program I need not to catch any exception and it automatically handles the error, though its Constructor of integer class throws NumberFormatException.
How can I achieve same thing as in my testExceptionprogram as has been achieved in CheckInteger where its constructor throws an error but I need not to explicitly catch the same.
regards,
Arun





[ December 04, 2002: Message edited by: Cindy Glass ]
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this question sounds stupid but I'd appreciate if someone answer this. Pl help...
regards,
Arun
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key is that NumberFormatException is descended from RuntimeException. From the API specification - " A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught."
So, to accomplish the behavior, extend RuntimeException.
HOWEVER, consider this carefully. RuntimeExceptions are generally used to handle (essentially) programming errors. It looks like your SmartMessageException might fit the bill, if that's the only way that that exception might be used (if the value passed in isn't one of the enumerated constants). But otherwise, if someone might create that SmartMessageException and really want it to be caught and dealt with, don't subclass RuntimeException.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use

public void startServer() throws Exception

then you won't have to catch it. Is that what you were asking?
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic