Here is a code snippet.
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 no exception handling is required while dir.mkdir()
when there is one required while file.createNewFile()
.
Are we very much sure that "Nothing could wrong" while creating a directory ? If yes, what are the reasons ?
Good question.
There really isn't a good reason for such different behavior.
The createNewFile()
was added to JDK in version 1.2 and the mkdir()
was added in 1.0. That's most likely the reason why API designers decide to make the newer functionality to throw the IOException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With