File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is wrong in this code ?

 
lakmal padmakumara
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • 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 !!!
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks It was really helpful !!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic