-2

I am learning how to create file and directory in java using this code. On the ERROR LINE I am getting error as "IOException is never thrown in this block".
So how do I know which function is throwing what type of Exception? Or if I am not sure I should use generic Exception in every catch block.

public class FileTest {
    public static void main(String[] args) {
        //file creation
        boolean flag = false;
        File file = new File("/IdeaProjects/JavaCode/jstest.txt");
        try {
            flag  = file.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        System.out.println("file path is : "  + file.getPath());

        //dir creation
        boolean dirFlag = false;
        File fileDir  = new File("/IdeaProjects/JavaCode/js");
        try{
            dirFlag = fileDir.mkdir();
        }catch (IOException e){//ERROR LINE
            e.printStackTrace();
        }
        if(dirFlag)
            System.out.println("created");
        else
            System.out.println("exist");
    }
}
js_248
  • 2,032
  • 4
  • 27
  • 38
  • You only have one method call in your try block. Read the Javadoc of that method to see what it may throw. – Eran Jun 09 '16 at 10:33

3 Answers3

1

The java.io.File#mkdir method only declares to throw SecurityException - see API.

java.lang.SecurityException is a RuntimeException and doesn't require being caught, although you may want to, depending on the context (again, see API).

Catching general java.lang.Exception in every catch block is absolutely not a recommended practice, although you may sometimes have to (not in your present case though).

See here for some SO literature on the matter.

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
  • It means I must see type of Exception thrown by a function from java API and write accordingly. – js_248 Jun 09 '16 at 10:43
  • @user3747720 well yes, consuming an API implies understanding what it does or is supposed to do. This is valid in general, for any programming language and any given context. – Mena Jun 09 '16 at 11:04
0
  • Remember what methods throw exceptions and which exceptions they are.
  • Check the documentation if you think a method may throw an exception.
  • Just attempt to compile the code and fix the errors the compiler throws (They will tell you what exceptions are thrown by what method if the try-catch block is missing).

The method in question (File.mkdir()) throws a SecurityException which doesn't need to be caught (you can if need be) as it is an unchecked RuntimeException.

java.io.File: https://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdir()

SecurityException: https://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.html

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
0

Quoting JLS Section 11.2:

It is a compile-time error if a catch clause can catch checked exception class E1 and it is not the case that the try block corresponding to the catch clause can throw a checked exception class that is a subclass or superclass of E1, unless E1 is Exception or a superclass of Exception.

If no method you invoke in the try block declares that it throws IOException (and you don't throw new IOException(..) directly either), it is a compile-time error if you try to catch an IOException.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243