aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is wrong in this code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is wrong in this code ?" Watch "What is wrong in this code ?" New topic
Author

What is wrong in this code ?

lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47

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 !!!

[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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

Joined: Aug 08, 2009
Posts: 47
Thanks It was really helpful !!
 
Consider Paul's rocket mass heater.
 
subject: What is wrong in this code ?