• 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:

What is wrong in this code ?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i want to throw an exception if an invalid input is given . But s gives an error @ line " catch (IOException e){" saying exception is never thrown in corresponding try statement .....cant we use IOException here ?? any suggestions ..please help..im learning java through self studies !!!
 
Sheriff
Posts: 9709
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
Hi lakmal, welcome to javaranch.

You can't catch IOException as it is not thrown in the code (as the error says). You can use Exception instead if IOException.

Also lakma, please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks i corrected it !!! but now i have another problem .....Please Explain me the difference of IOException and Exception.
And also when im using Exception im facing a problem .Beacuse a String can be a number also. So when i enter a number as my name it does not run the exception !! tell me a solution for that ! thanks ! (for name accepting only letters )
 
Ankit Garg
Sheriff
Posts: 9709
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

lakmal padmakumara wrote:Please Explain me the difference of IOException and Exception.



Exception and IOException both are checked exceptions, but Exception class has an unchecked sub-class RuntimeException. So when you catch an IOException, then the compiler checks that there should be an IOException thrown in the associated try block. If there is a chance that IOException is thrown in the try block it is fine, otherwise you'll get a compilation error. The compiler only checks this for checked exceptions (as is clear from the name). This is why this check is not performed for Exception (although Exception class itself is checked, it has an unchecked sub-class so the check is not performed)

lakmal padmakumara wrote:And also when im using Exception im facing a problem .Beacuse a String can be a number also. So when i enter a number as my name it does not run the exception !! tell me a solution for that ! thanks ! (for name accepting only letters )



If you want to disallow numbers, then you'll have to manually check the string by applying a regular expression on it...
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks It was really helpful !!
 
reply
    Bookmark Topic Watch Topic
  • New Topic