Why dir.mkdir () does not require exception handling when file.createNewFile () does?

Here is a snippet of code.

File dir = new File("dir"); dir.mkdir(); File file = new File(dir,"file.txt"); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

I want to know why exception handling is not required during dir.mkdir() , when only file.createNewFile() is required.

Are we very sure that “nothing happened” when creating the catalog? If so, what are the reasons?

+7
java file file-io
source share
1 answer

Good question.

In fact, there is no good reason for this behavior.

createNewFile() was added in the JDK in version 1.2, and mkdir() was added in 1.0. Most likely, the reason API designers decide to make the new functionality throw an IOException .

+5
source share

All Articles