It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes catch block does not compile 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 "catch block does not compile" Watch "catch block does not compile" New topic

catch block does not compile

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

Generally whatever we write in the try block is actually tried to execute and if it causes some exception, then catch block comes to help but even if the try block does not throw exception, the compiler does not complain, but in my case, I want to create a directory and I am writing the code for that in try block but the compiler says something interesting as below
here is the code

and when program is compiled, then error is
F:\Java\Concepts\Strings,IO Formatting and Parsing\File IO>javac CreateFileAndDi exception is never thrown in
body of corresponding try statement
catch (IOException e)
1 error

but this does not happen in case of CreateFile() method, means it compiles fine but in case of CreateDirctury, it does not create, so I commented out the code for catch block
there is another example ,
here is simple code that compiles fine even if the try block does not throw any kind of exception

This compiler perfectly
what can be the difference?
I am finding it very very interesting
please help experts

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Maybe it's because the method createNewFile() in File throws an IOException (it's not a RuntimeException (so, it must be catched), like ArithmeticException and NullPointerException, that only can be detected at runtime) and the mkdir() method doesn't throw that.

Exception description: wrote:The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

And RuntimeException: wrote: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.

I guess that's the reason why...
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
I am not sure if I completely understand your question, but let me try to give a shot at the answer:

- mkdir() doesn't throw an IOException. So you cannot have a catch block to catch an IOException for mkdir(). If you have one, the compiler will complain. Why the mkdir() doesn't throw an IOException is beyond me. I too would like to understand why it was designed that way.

- createNewFile() DOES throw an IOException. So you have to catch this Exception in a catch block (or declare that the method throws an IOException). Otherwise the compiler will complain.

- In your last example, you are catching an ArithmeticException which is a RuntimeException. In this case the code in the try block doesn't really have to throw a RuntimeException. You are free to catch any RuntimeException in a catch block, irrespective of whether your code in the try block throws one or not.

Hope this helps.

teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51

teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
program is working fine with out any and compile again .Your code is correct its working.
I think you might have compiled the program like this

dirmkdir() doesn't throws any exception and more over you can not use checked exception for try catch if not the corresponding code /doesn't throws checked exception.

[Devaka: Added code tags - Please UseCodeTags]
subject: catch block does not compile
Similar Threads
store a file in tomcat server
error in try catch
which catch start first ?
catch block.....
Can we use finaly block to catch exceptions without using catch block?