Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why dir.mkdir() requires no exception handling, when file.createNewFile() does?

Tags:

java

file

file-io

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 ?

like image 423
Abhishek Singh Avatar asked Mar 22 '15 07:03

Abhishek Singh


1 Answers

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.

like image 154
Crazyjavahacking Avatar answered Sep 28 '22 23:09

Crazyjavahacking